获取Android设备名称

发布于 2024-11-29 20:59:23 字数 132 浏览 0 评论 0原文

如何获取Android设备名称?我正在使用 HTC 希望。当我通过 HTC Sync 连接它时,软件显示名称 'HTC Smith' 。我想通过代码获取这个名字。

这在 Android 中怎么可能呢?

How to get Android device name? I am using HTC desire. When I connected it via HTC Sync the software is displaying the Name 'HTC Smith' . I would like to fetch this name via code.

How is this possible in Android?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

蓝天 2024-12-06 20:59:23

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

android.os.Build.MODEL;

可在此处找到:获取 Android 设备名称

In order to get Android device name you have to add only a single line of code:

android.os.Build.MODEL;

Found here: getting-android-device-name

深陷 2024-12-06 20:59:23

您可以在此处查看答案以编程方式获取 Android 手机型号

public String getDeviceName() {
   String manufacturer = Build.MANUFACTURER;
   String model = Build.MODEL;
   if (model.startsWith(manufacturer)) {
      return capitalize(model);
   } else {
      return capitalize(manufacturer) + " " + model;
   }
}


private String capitalize(String s) {
    if (s == null || s.length() == 0) {
        return "";
    }
    char first = s.charAt(0);
    if (Character.isUpperCase(first)) {
        return s;
    } else {
        return Character.toUpperCase(first) + s.substring(1);
    }
}

You can see answers at here Get Android Phone Model Programmatically

public String getDeviceName() {
   String manufacturer = Build.MANUFACTURER;
   String model = Build.MODEL;
   if (model.startsWith(manufacturer)) {
      return capitalize(model);
   } else {
      return capitalize(manufacturer) + " " + model;
   }
}


private String capitalize(String s) {
    if (s == null || s.length() == 0) {
        return "";
    }
    char first = s.charAt(0);
    if (Character.isUpperCase(first)) {
        return s;
    } else {
        return Character.toUpperCase(first) + s.substring(1);
    }
}
雪化雨蝶 2024-12-06 20:59:23

我通过获取蓝牙名称解决了这个问题,但不是从 BluetoothAdapter (需要蓝牙权限)获取。

代码如下:

Settings.Secure.getString(getContentResolver(), "bluetooth_name");

无需额外权限。

UPD:它不适用于 targetSdk 32 或更高版本。

I solved this by getting the Bluetooth name, but not from the BluetoothAdapter (that needs Bluetooth permission).

Here's the code:

Settings.Secure.getString(getContentResolver(), "bluetooth_name");

No extra permissions needed.

UPD: It doesn't work on targetSdk 32 or higher.

在风中等你 2024-12-06 20:59:23

在许多流行的设备上,该设备的市场名称不可用。例如,在 Samsung Galaxy S6 上,Build.MODEL 的值可能是 "SM-G920F""SM-G920I"“SM-G920W8”

我创建了一个小型库,用于获取设备的市场(消费者友好)名称。它为超过 10,000 设备获取正确的名称,并且不断更新。如果您想使用我的库,请单击以下链接:

Github 上的 AndroidDeviceNames 库


如果您不想使用上面的库,那么这是获得消费者友好的设备名称的最佳解决方案:

/** Returns the consumer friendly device name */
public static String getDeviceName() {
    String manufacturer = Build.MANUFACTURER;
    String model = Build.MODEL;
    if (model.startsWith(manufacturer)) {
        return capitalize(model);
    }
    return capitalize(manufacturer) + " " + model;
}

private static String capitalize(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    char[] arr = str.toCharArray();
    boolean capitalizeNext = true;
    String phrase = "";
    for (char c : arr) {
        if (capitalizeNext && Character.isLetter(c)) {
            phrase += Character.toUpperCase(c);
            capitalizeNext = false;
            continue;
        } else if (Character.isWhitespace(c)) {
            capitalizeNext = true;
        }
        phrase += c;
    }
    return phrase;
}

以我的 Verizon HTC One M8 为例:

// using method from above
System.out.println(getDeviceName());
// Using https://github.com/jaredrummler/AndroidDeviceNames
System.out.println(DeviceName.getDeviceName());

结果:

HTC6525LVW

HTC One (M8)

On many popular devices the market name of the device is not available. For example, on the Samsung Galaxy S6 the value of Build.MODEL could be "SM-G920F", "SM-G920I", or "SM-G920W8".

I created a small library that gets the market (consumer friendly) name of a device. It gets the correct name for over 10,000 devices and is constantly updated. If you wish to use my library click the link below:

AndroidDeviceNames Library on Github


If you do not want to use the library above, then this is the best solution for getting a consumer friendly device name:

/** Returns the consumer friendly device name */
public static String getDeviceName() {
    String manufacturer = Build.MANUFACTURER;
    String model = Build.MODEL;
    if (model.startsWith(manufacturer)) {
        return capitalize(model);
    }
    return capitalize(manufacturer) + " " + model;
}

private static String capitalize(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    char[] arr = str.toCharArray();
    boolean capitalizeNext = true;
    String phrase = "";
    for (char c : arr) {
        if (capitalizeNext && Character.isLetter(c)) {
            phrase += Character.toUpperCase(c);
            capitalizeNext = false;
            continue;
        } else if (Character.isWhitespace(c)) {
            capitalizeNext = true;
        }
        phrase += c;
    }
    return phrase;
}

Example from my Verizon HTC One M8:

// using method from above
System.out.println(getDeviceName());
// Using https://github.com/jaredrummler/AndroidDeviceNames
System.out.println(DeviceName.getDeviceName());

Result:

HTC6525LVW

HTC One (M8)

锦上情书 2024-12-06 20:59:23

尝试一下。您可以通过蓝牙获取设备名称。

希望它能帮助你

public String getPhoneName() {  
        BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
        String deviceName = myDevice.getName();     
        return deviceName;
    }

Try it. You can get Device Name through Bluetooth.

Hope it will help you

public String getPhoneName() {  
        BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
        String deviceName = myDevice.getName();     
        return deviceName;
    }
橘香 2024-12-06 20:59:23

您可以使用:

来自 android 文档:

制造商

字符串制造商

产品/硬件的制造商。

模型

字符串模型

最终产品的最终用户可见的名称。

DEVICE

字符串设备

工业品外观设计的名称。

举个例子:

String deviceName = android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL;
//to add to textview
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(deviceName);

此外,Build class 中有很多属性您可以使用,例如:

  • os.android.Build.BOARD
  • os.android.Build.BRAND
  • os.android.Build.BOOTLOADER
  • os.android.Build.DISPLAY
  • os.android.Build.CPU_ABI
  • os.android.Build.Product
  • os.android.Build.HARDWARE
  • os.android.Build .ID

此外,您还可以通过其他方式获取设备名称,而无需使用 Build 类(通过蓝牙)。

You can use:

From android doc:

MANUFACTURER:

String MANUFACTURER

The manufacturer of the product/hardware.

MODEL:

String MODEL

The end-user-visible name for the end product.

DEVICE:

String DEVICE

The name of the industrial design.

As a example:

String deviceName = android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL;
//to add to textview
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(deviceName);

Furthermore, their is lot of attribute in Build class that you can use, like:

  • os.android.Build.BOARD
  • os.android.Build.BRAND
  • os.android.Build.BOOTLOADER
  • os.android.Build.DISPLAY
  • os.android.Build.CPU_ABI
  • os.android.Build.PRODUCT
  • os.android.Build.HARDWARE
  • os.android.Build.ID

Also their is other ways you can get device name without using Build class(through the bluetooth).

£噩梦荏苒 2024-12-06 20:59:23

以下对我有用。

String deviceName = Settings.Global.getString(.getContentResolver(), Settings.Global.DEVICE_NAME);

我不认为这是重复的答案。上面的人正在谈论设置安全,对我来说设置安全是给 null,如果我使用设置全局它可以工作。无论如何,谢谢。

Following works for me.

String deviceName = Settings.Global.getString(.getContentResolver(), Settings.Global.DEVICE_NAME);

I don't think so its duplicate answer. The above ppl are talking about Setting Secure, for me setting secure is giving null, if i use setting global it works. Thanks anyways.

时光与爱终年不遇 2024-12-06 20:59:23

用户定义的DeviceName适用于几乎所有设备并且不需要任何权限的通用方法

String userDeviceName = Settings.Global.getString(getContentResolver(), Settings.Global.DEVICE_NAME);
if(userDeviceName == null)
    userDeviceName = Settings.Secure.getString(getContentResolver(), "bluetooth_name");

universal way to get user defined DeviceName working for almost all devices and not requiring any permissions

String userDeviceName = Settings.Global.getString(getContentResolver(), Settings.Global.DEVICE_NAME);
if(userDeviceName == null)
    userDeviceName = Settings.Secure.getString(getContentResolver(), "bluetooth_name");
酒绊 2024-12-06 20:59:23

试试这个代码。您将获得 Android 设备名称。

public static String getDeviceName() {
    String manufacturer = Build.MANUFACTURER;
    String model = Build.MODEL;
    if (model.startsWith(manufacturer)) {
        return model;
    }
    return manufacturer + " " + model;
}

Try this code. You get android device name.

public static String getDeviceName() {
    String manufacturer = Build.MANUFACTURER;
    String model = Build.MODEL;
    if (model.startsWith(manufacturer)) {
        return model;
    }
    return manufacturer + " " + model;
}
橙幽之幻 2024-12-06 20:59:23

@hbhakhra 的答案就可以了。

如果您对详细说明感兴趣,请查看 Android 兼容性定义文档。 (3.2.2 构建参数)

你会发现:

DEVICE - 设备实现者选择的值,包含
标识配置的开发名称或代码名称
设备的硬件特性和工业设计。的价值
该字段必须可编码为 7 位 ASCII 并与常规字段匹配
表达式“^[a-zA-Z0-9_-]+$”。

MODEL - 设备实现者选择的包含名称的值
最终用户已知的设备的信息。这应该是同一个名字
设备根据该规则进行营销并销售给最终用户。没有
对此字段的具体格式有要求,但必须
不能为 null 或空字符串 ("")。

制造商 - 原始设备制造商的商品名称
(OEM)产品。具体格式没有要求
该字段的,但它不能为 null 或空字符串
(“”)。

@hbhakhra's answer will do.

If you're interested in detailed explanation, it is useful to look into Android Compatibility Definition Document. (3.2.2 Build Parameters)

You will find:

DEVICE - A value chosen by the device implementer containing the
development name or code name identifying the configuration of the
hardware features and industrial design of the device. The value of
this field MUST be encodable as 7-bit ASCII and match the regular
expression “^[a-zA-Z0-9_-]+$”.

MODEL - A value chosen by the device implementer containing the name
of the device as known to the end user. This SHOULD be the same name
under which the device is marketed and sold to end users. There are no
requirements on the specific format of this field, except that it MUST
NOT be null or the empty string ("").

MANUFACTURER - The trade name of the Original Equipment Manufacturer
(OEM) of the product. There are no requirements on the specific format
of this field, except that it MUST NOT be null or the empty string
("").

清泪尽 2024-12-06 20:59:23

更新
您可以轻松地从 buildprop 检索设备。

static String GetDeviceName() {
    Process p;
    String propvalue = "";
    try {
        p = new ProcessBuilder("/system/bin/getprop", "ro.semc.product.name").redirectErrorStream(true).start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            propvalue = line;
        }
        p.destroy();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return propvalue;
}

但请记住,这在某些设备上不起作用。

UPDATE
You could retrieve the device from buildprop easitly.

static String GetDeviceName() {
    Process p;
    String propvalue = "";
    try {
        p = new ProcessBuilder("/system/bin/getprop", "ro.semc.product.name").redirectErrorStream(true).start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            propvalue = line;
        }
        p.destroy();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return propvalue;
}

But keep in mind, this doesn't work on some devices.

音栖息无 2024-12-06 20:59:23

只需使用

BluetoothAdapter.getDefaultAdapter().getName()

Simply use

BluetoothAdapter.getDefaultAdapter().getName()

稚气少女 2024-12-06 20:59:23
 static String getDeviceName() {
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method getMethod = systemPropertiesClass.getMethod("get", String.class);
            Object object = new Object();
            Object obj = getMethod.invoke(object, "ro.product.device");
            return (obj == null ? "" : (String) obj);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

你可以通过这种方式得到'idol3'。

 static String getDeviceName() {
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method getMethod = systemPropertiesClass.getMethod("get", String.class);
            Object object = new Object();
            Object obj = getMethod.invoke(object, "ro.product.device");
            return (obj == null ? "" : (String) obj);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

enter image description here

you can get 'idol3' by this way.

番薯 2024-12-06 20:59:23

尝试过这些库,但没有任何效果符合我的预期,并且给出了绝对错误的名称。

所以我使用相同的数据自己创建了这个库。
这是链接

AndroidPhoneNamesFinder

要使用此库,只需添加此库即可实现

implementation 'com.github.aishik212:AndroidPhoneNamesFinder:v1.0.2'

然后使用以下 kotlin 代码

DeviceNameFinder.getPhoneValues(this, object : DeviceDetailsListener
{  
    override fun details(doQuery: DeviceDetailsModel?) 
    {  
        super.details(doQuery)  
        Log.d(TAG, "details: "+doQuery?.calculatedName)  
    }  
})

这些是 DeviceDetailsModel 示例中获得的值-

val brand: String? #This is the brandName of the Device  
val commonName: String?, #This is the most common Name of the Device  
val codeName: String?,  #This is the codeName of the Device
val modelName: String?,  #This is the another uncommon Name of the Device
val calculatedName: String?, #This is the special name that this library tries to create from the above data.

您将从Android 模拟器的

brand=Google 
commonName=Google Android Emulator 
codeName=generic_x86_arm 
modelName=sdk_gphone_x86 
calculatedName=Google Android Emulator

Tried These libraries but nothing worked according to my expectation and was giving absolutely wrong names.

So i created this library myself using the same data.
Here is the link

AndroidPhoneNamesFinder

To use this library just add this for implementation

implementation 'com.github.aishik212:AndroidPhoneNamesFinder:v1.0.2'

Then use the following kotlin code

DeviceNameFinder.getPhoneValues(this, object : DeviceDetailsListener
{  
    override fun details(doQuery: DeviceDetailsModel?) 
    {  
        super.details(doQuery)  
        Log.d(TAG, "details: "+doQuery?.calculatedName)  
    }  
})

These are the values you will get from DeviceDetailsModel

val brand: String? #This is the brandName of the Device  
val commonName: String?, #This is the most common Name of the Device  
val codeName: String?,  #This is the codeName of the Device
val modelName: String?,  #This is the another uncommon Name of the Device
val calculatedName: String?, #This is the special name that this library tries to create from the above data.

Example of Android Emulator -

brand=Google 
commonName=Google Android Emulator 
codeName=generic_x86_arm 
modelName=sdk_gphone_x86 
calculatedName=Google Android Emulator
撑一把青伞 2024-12-06 20:59:23
  1. 获取 Android 系统属性,或列出所有属性
adb shell getprop >prop_list.txt
  1. 在 prop_list.txt 中查找您的设备名称以获取属性名称,

例如我的设备名称是ro.oppo.market.name

  1. 获取oppo.market运营商
adb shell getprop ro.oppo.market.name
  1. 我在windows上的情况如下
D:\winusr\adbl

λ *adb shell getprop ro.oppo.market.name*

OPPO R17

  1. Gets an Android system property, or lists them all
adb shell getprop >prop_list.txt
  1. Find your device name in prop_list.txt to get the prop name

e.g. my device name is ro.oppo.market.name

  1. Get oppo.market Operator
adb shell getprop ro.oppo.market.name
  1. My case on windows as follows
D:\winusr\adbl

λ *adb shell getprop ro.oppo.market.name*

OPPO R17

赢得她心 2024-12-06 20:59:23

在 Android 的 GNU/Linux 环境中,例如,通过 Termux UNIX shell非根设备,可以通过 /system/bin/getprop 命令获取,而每个值的含义在 构建Android 中的 .java(也位于 googlesource):

% /system/bin/getprop | fgrep ro.product | tail -3
[ro.product.manufacturer]: [Google]
[ro.product.model]: [Pixel 2 XL]
[ro.product.name]: [taimen]
% /system/bin/getprop ro.product.model
Pixel 2 XL
% /system/bin/getprop ro.product.model | tr ' ' _
Pixel_2_XL

例如,可以设置为 tmux 像这样:

tmux select-pane -T "$(getprop ro.product.model)"

Within the GNU/Linux environment of Android, e.g., via Termux UNIX shell on a non-root device, it's available through the /system/bin/getprop command, whereas the meaning of each value is explained in Build.java within Android (also at googlesource):

% /system/bin/getprop | fgrep ro.product | tail -3
[ro.product.manufacturer]: [Google]
[ro.product.model]: [Pixel 2 XL]
[ro.product.name]: [taimen]
% /system/bin/getprop ro.product.model
Pixel 2 XL
% /system/bin/getprop ro.product.model | tr ' ' _
Pixel_2_XL

For example, it can be set as the pane_title for the status-right within tmux like so:

tmux select-pane -T "$(getprop ro.product.model)"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文