如何在Android中以编程方式获取设备名称?
如何在Android中以编程方式获取设备名称?
TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);
您可以使用android.provider.Settings来获取设备名称:
Settings.Global.getString(context.getContentResolver(), "device_name")
android.os.Build.MANUFACTURER + android.os.Build.MODEL
如其他人所提到的,您可以使用Build.MODEL
、Build.DEVICE
等方式,获取有关设备的更多信息。请访问developer.android.com了解有关Build
类的更多信息。BTW,请记得导入/添加import android.os.Build;
。
一个简单的示例:
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();
只需添加一行代码:
android.os.Build.MODEL;
将以下代码复制并粘贴到您的项目的onCreate()
中:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);
要获取手机的名称,例如:Galaxy S5或类似的名称,只需在依赖项中添加以下内容:
compile 'com.jaredrummler:android-device-names:1.0.9'
接下来同步您的项目,当其完成后,进入您的Java类并使用以下代码:
String mDeviceName = DeviceName.getDeviceName();
就是这样。
String manufacturerModel = android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL;
使用以下方式可以获取设备名称,而且在用户修改设备名称的情况下,也可以获取到更新后的名称:
Settings.Secure.getString(getContentResolver(), “bluetooth_name”);
Settings.System.getString(getContentResolver(), “device_name”);
使用adb获取用户在“关于手机”“设备名称”屏幕中设置的名称的方法如下:
$ adb shell 'settings get global device_name'
settings list global
可以显示其他可用设置的列表。 - undefined