如何在Android中获取设备名称?

Can*_*ner 47 android

如何在Android中以编程方式获取设备名称?

Rah*_*rma 68

要在android使用中显示设备名称/型号:

TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);
Run Code Online (Sandbox Code Playgroud)

链接到Android Build Class

  • 这是型号,不是设备名称 (3认同)

小智 20

android.os.Build.MANUFACTURER + android.os.Build.MODEL
Run Code Online (Sandbox Code Playgroud)

  • 在LG制造的Google Nexus设备中-这将为您提供错误的字符串-“ LGE Nexus 5”。您应该使用android.os.Build.BRAND + android.os.Build.MODEL。 (2认同)

Bla*_*nka 8

正如其他人提到的,你可以使用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();
Run Code Online (Sandbox Code Playgroud)


Rup*_*pok 5

为了获得Android设备名称,您只需添加一行代码:

android.os.Build.MODEL;
Run Code Online (Sandbox Code Playgroud)

将以下代码复制并粘贴到项目中onCreate():

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);
Run Code Online (Sandbox Code Playgroud)


小智 5

要获取手机名称,例如:Galaxy S5 或类似名称,只需将其添加到您的依赖项中:

compile 'com.jaredrummler:android-device-names:1.0.9'
Run Code Online (Sandbox Code Playgroud)

然后同步您的项目,完成后,转到您的 Java 类并使用以下代码:

String mDeviceName = DeviceName.getDeviceName();
Run Code Online (Sandbox Code Playgroud)

就是这样。


Ber*_*tel 5

您可以使用以下代码从android.provider.Settings获取设备名称:

Settings.Global.getString(context.getContentResolver(), "device_name")
Run Code Online (Sandbox Code Playgroud)