How can I get the device name in Android?
开发者_如何学编程How can I get the device name programmatically in Android?
To display the device name/model in android use:
TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);
Link to Android Build Class
android.os.Build.MANUFACTURER + android.os.Build.MODEL
You can get the device name from android.provider.Settings using :
Settings.Global.getString(context.getContentResolver(), "device_name")
As others mentioned, you can use Build.MODEL
, Build.DEVICE
and you can get lot more info about device. Go to developer.android.com to read more about Build
class. BTW remember to import/add import android.os.Build;
.
A simple example:
StringBuffer infoBuffer = new StringBuffer();
infoBuffer.append("-------------------------------------\n");
infoBuffer.append("Model :" + Build.MODEL + "\n");//The end-user-visible name for the end product.
infoBuffer.append("Device: " + Build.DEVICE + "\n");//The name of the industrial design.
infoBuffer.append("Manufacturer: " + Build.MANUFACTURER + "\n");//The manufacturer of the product/hardware.
infoBuffer.append("Board: " + Build.BOARD + "\n");//The name of the underlying board, like "goldfish".
infoBuffer.append("Brand: " + Build.BRAND + "\n");//The consumer-visible brand with which the product/hardware will be associated, if any.
infoBuffer.append("Serial: " + Build.SERIAL + "\n");
infoBuffer.append("-------------------------------------\n");
/* Android doc:
This 'Serial' field was deprecated in API level O.
Use getSerial() instead.
A hardware serial number, if available.
Alphanumeric only, case-insensitive. For apps targeting SDK higher than N_MR1 this field is set to UNKNOWN.
*/
//I just used AlertDialog to show device information
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(true);
dialog.setTitle("Device information:");
dialog.setMessage(infoBuffer);//StringBuffer which we appended the device informations.
dialog.show();
In order to get android device name you have to add only a single line of code:
android.os.Build.MODEL;
Copy and paste the following code in your projects onCreate()
:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);
To get the phone name, for instance: Galaxy S5 or the like, just add this to your dependencies:
compile 'com.jaredrummler:android-device-names:1.0.9'
Then sync your project, when it finishes, go to your Java class and use this code:
String mDeviceName = DeviceName.getDeviceName();
And that's it.
The name and model of your Android Device can be found like this:
String manufacturerModel = android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL;
Using following way can get the device name and also in case user modify the device name, it can also get the updated name:
Settings.Secure.getString(getContentResolver(), “bluetooth_name”);
Because bluetooth_name
is not a documented API, in case it returns null
, you can use following code instead: (note that this API is only available since Android 7.1 and per my testing, it CANNOT get the device name immediately after user modified)
Settings.System.getString(getContentResolver(), “device_name”);
Here is how to get the name that the user set in the "About Phone" "Device name" screen using adb:
$ adb shell 'settings get global device_name'
精彩评论