简体   繁体   English

获取 Android 设备名称

[英]Get Android Device Name

How to get Android device name?如何获取 Android 设备名称? I am using HTC desire.我正在使用 HTC 欲望。 When I connected it via HTC Sync the software is displaying the Name 'HTC Smith' .当我通过 HTC Sync 连接它时,软件显示名称“HTC Smith” I would like to fetch this name via code.我想通过代码获取这个名字。

How is this possible in Android?这在 Android 中怎么可能?

In order to get Android device name you have to add only a single line of code:为了获得 Android 设备名称,您只需添加一行代码:

android.os.Build.MODEL;

Found here: getting-android-device-name在这里找到: getting-android-device-name

You can see answers at here Get Android Phone Model Programmatically您可以在此处查看答案 以编程方式获取 Android 电话 Model

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);
    }
}

I solved this by getting the Bluetooth name, but not from the BluetoothAdapter (that needs Bluetooth permission).我通过获取蓝牙名称解决了这个问题,但不是从BluetoothAdapter (需要蓝牙权限)。

Here's the code:这是代码:

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

No extra permissions needed.不需要额外的权限。

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" .例如,在三星 Galaxy S6 上, Build.MODEL的值可以是"SM-G920F""SM-G920I""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.它为超过 10,000台设备获得了正确的名称,并且不断更新。 If you wish to use my library click the link below:如果您想使用我的图书馆,请单击下面的链接:

AndroidDeviceNames Library on Github Github 上的 AndroidDeviceNames 库


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:我的 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 HTC6525LVW

HTC One (M8)宏达一 (M8)

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;
    }

You can use:您可以使用:

From android doc:来自 android 文档:

MANUFACTURER : MANUFACTURER

 String MANUFACTURER

The manufacturer of the product/hardware.产品/硬件的制造商。

MODEL : MODEL

 String MODEL

The end-user-visible name for the end product.最终产品的最终用户可见名称。

DEVICE : 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:此外,它们在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

Also their is other ways you can get device name without using Build class(through the bluetooth).他们还有其他方法可以在不使用Build类(通过蓝牙)的情况下获取设备名称。

Following works for me.以下对我有用。

String deviceName = Settings.Global.getString(.getContentResolver(), Settings.Global.DEVICE_NAME); 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.上面的人在谈论设置安全,对我来说设置安全是给 null,如果我使用设置全局它可以工作。 Thanks anyways.不管怎么说,多谢拉。

@hbhakhra's answer will do. @hbhakhra 的回答会做。

If you're interested in detailed explanation, it is useful to look into Android Compatibility Definition Document .如果您对详细解释感兴趣,查看Android 兼容性定义文档会很有用。 (3.2.2 Build Parameters) (3.2.2 编译参数)

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. DEVICE - 由设备实现者选择的一个值,包含标识硬件功能配置和设备工业设计的开发名称或代号。 The value of this field MUST be encodable as 7-bit ASCII and match the regular expression “^[a-zA-Z0-9_-]+$”.此字段的值必须可编码为 7 位 ASCII 并匹配正则表达式“^[a-zA-Z0-9_-]+$”。

MODEL - A value chosen by the device implementer containing the name of the device as known to the end user. MODEL - 由设备实现者选择的值,其中包含最终用户已知的设备名称。 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 ("").该字段的具体格式没有要求,但不得为 null 或空字符串(“”)。

MANUFACTURER - The trade name of the Original Equipment Manufacturer (OEM) of the product.制造商 - 产品的原始设备制造商 (OEM) 的商品名称。 There are no requirements on the specific format of this field, except that it MUST NOT be null or the empty string ("").该字段的具体格式没有要求,但不得为 null 或空字符串(“”)。

Simply use只需使用

BluetoothAdapter.getDefaultAdapter().getName()

UPDATE You could retrieve the device from buildprop easitly.更新您可以轻松地从 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;
}

But keep in mind, this doesn't work on some devices.但请记住,这在某些设备上不起作用。

Try this code.试试这个代码。 You get android device name.您将获得 android 设备名称。

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

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 AndroidPhoneNamesFinder

To use this library just add this for implementation要使用这个库只需添加这个来实现

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

Then use the following kotlin code然后使用下面的kotlin代码

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这些是您将从 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 - Android 模拟器示例 -

brand=Google 
commonName=Google Android Emulator 
codeName=generic_x86_arm 
modelName=sdk_gphone_x86 
calculatedName=Google Android Emulator
 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 "";
        }
    }

在此处输入图像描述

you can get 'idol3' by this way.你可以通过这种方式获得'idol3'。

universal way to get user defined DeviceName working for almost all devices and not requiring any permissions使用户定义的 DeviceName适用于几乎所有设备且不需要任何权限的通用方法

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

Within the GNU/Linux environment of Android, eg, 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 ): Within the GNU/Linux environment of Android, eg, 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在谷歌来源):

% /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:例如,它可以设置为tmuxstatus-rightpane_title如下所示:

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

First,第一的,

adb shell getprop >prop_list.txt adb shell getprop >prop_list.txt

Second,第二,

find your device name in prop_list.txt to get the prop name, eg my device name is ro.oppo.market.name在 prop_list.txt 中找到您的设备名称以获取道具名称,例如我的设备名称是 ro.oppo.market.name

Finally,最后,

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

D:\winusr\adbl D:\winusr\adbl

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

OPPO R17 OPPO R17

D:\winusr\adbl D:\winusr\adbl

λ λ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM