如何在Android中以编程方式获取设备名称?
答案 0 :(得分:66)
要在android使用中显示设备名称/型号:
TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);
答案 1 :(得分:19)
android.os.Build.MANUFACTURER + android.os.Build.MODEL
答案 2 :(得分:6)
正如其他人提到的,您可以使用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();
答案 3 :(得分:5)
为了获得Android设备名称,您只需添加一行代码:
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);
答案 4 :(得分:3)
要获取电话名称,例如:Galaxy S5等,只需将其添加到您的依赖项中:
compile 'com.jaredrummler:android-device-names:1.0.9'
然后同步你的项目,完成后,转到你的Java类并使用这段代码:
String mDeviceName = DeviceName.getDeviceName();
就是这样。
答案 5 :(得分:2)
您可以使用以下命令从android.provider.Settings获取设备名称:
Settings.Global.getString(context.getContentResolver(), "device_name")
答案 6 :(得分:1)
您可以通过以下方式找到Android设备的名称和型号:
String manufacturerModel = android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL;
答案 7 :(得分:0)
使用以下方式可以获得设备名称,如果用户修改了设备名称,也可以获得更新后的名称:
Settings.Secure.getString(getContentResolver(), “bluetooth_name”);
因为 bluetooth_name
是没有文档化的 API,如果它返回 null
,您可以使用以下代码:(请注意,此 API 仅可用 从 Android 7.1 开始,根据我的测试,它不能在用户修改后立即获取设备名称)
Settings.System.getString(getContentResolver(), “device_name”);