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

57

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


2
请查看此链接:http://developer.android.com/reference/android/os/Build.html - Aravindhan
4
可能是重复的问题:如何在程序中获取 Android 手机型号? - kabuko
9个回答

76
在Android中显示设备名称/型号,请使用以下代码:
TextView tv1 = (TextView) findViewById(R.id.tv1);
String str = android.os.Build.MODEL;
tv1.setText(str);

链接到Android Build类


有没有什么解决方法可以获得名称而不是型号,例如三星Galaxy S。因为没有人知道型号,只有人们知道设备的名称。 - Piyush Agarwal
看下面我的答案,以获取制造商的详细信息。 - diptia
3
这是模型,不是设备名称。 - pixel

32

您可以使用android.provider.Settings来获取设备名称:

Settings.Global.getString(context.getContentResolver(), "device_name")

1
这应该是被接受的答案,因为它提供了用户可以修改的实际设备名称。其他答案只给出了设备制造商名称和设备型号。 - ljtomev
这需要支持的最低安卓版本是什么? - Amos
Settings.Global.getString(requireContext().contentResolver, Settings.Global.DEVICE_NAME),Android Studio 告诉我最低 API 级别为 25。 - Amos

31
android.os.Build.MANUFACTURER + android.os.Build.MODEL

3
在由LG制造的Google Nexus设备中,这将给你错误的字符串 - “LGE Nexus 5”。你应该使用android.os.Build.BRAND + android.os.Build.MODEL。 - FunkSoulBrother

12

如其他人所提到的,您可以使用Build.MODELBuild.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();

6
为了获取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);

这段代码是用于获取设备名称的,我该如何获取设备配置文件名称? - Amitsharma

6

要获取手机的名称,例如:Galaxy S5或类似的名称,只需在依赖项中添加以下内容:

compile 'com.jaredrummler:android-device-names:1.0.9'

接下来同步您的项目,当其完成后,进入您的Java类并使用以下代码:

String mDeviceName = DeviceName.getDeviceName();

就是这样。


4
您的Android设备的名称和型号可以通过以下方式找到:
String manufacturerModel = android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL;

Build.Model通常是一种晦涩的模型名称;例如,三星SM-T395是TabActive 2。因此,如果将其用于向用户显示,则答案是精确但不太用户友好的。 - FrankKrumnow

2

使用以下方式可以获取设备名称,而且在用户修改设备名称的情况下,也可以获取到更新后的名称

Settings.Secure.getString(getContentResolver(), “bluetooth_name”); 

由于`bluetooth_name`不是一个公开的API,如果它返回空值,你可以使用以下代码替代:(请注意,该API仅在Android 7.1及以上版本可用,并且经过测试,在用户修改后无法立即获取设备名称)
Settings.System.getString(getContentResolver(), “device_name”);

1

使用adb获取用户在“关于手机”“设备名称”屏幕中设置的名称的方法如下:

$ adb shell 'settings get global device_name'

使用命令 settings list global 可以显示其他可用设置的列表。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接