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

这在Android中是如何实现的呢?


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

android.os.Build.MODEL;

找到这里:getting-android-device-name


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

希望对你有所帮助

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

你可以在这里看到答案获得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);
    }
}

在许多流行的设备上,该设备的市场名称是不可用的。例如,在三星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)


@hbhakhra的答案就可以了。

如果你对详细的解释感兴趣,看看Android兼容性定义文档是很有用的。(3.2.2构建参数)

你会发现:

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 ("").


简单地使用

BluetoothAdapter.getDefaultAdapter () . getname ()


你可以使用:

来自android doc:

制造商: 字符串制造商 产品/硬件的制造商。 模型: 字符串模型 最终产品的最终用户可见的名称。 设备: 字符串的设备 工业设计的名称。

举个例子:

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类中有很多你可以使用的属性,比如:

android操作系统。棋盘构建。 android操作系统。布兰德构建。 android操作系统。BOOTLOADER构建。 显示屏构建android操作系统。。 android操作系统。CPU_ABI构建。 android os。构建的广告。 os硬件构建android。 android操作系统。ID构建。

还有其他方法可以在不使用Build类的情况下获取设备名称(通过蓝牙)。


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

代码如下:

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

不需要额外的权限。


更新 你可以很容易地从建筑道具中取回设备。

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

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


以下对我有用。

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

我不认为这是重复的答案。上面的ppl正在谈论设置安全,为我设置安全是给空,如果我使用设置全局它工作。不管怎样,谢谢你。


试试这段代码。你会得到android设备名。

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

 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'。


通用的方法,让用户定义的DeviceName工作几乎所有设备,不需要任何权限

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

在Android的GNU/Linux环境中,例如,通过非根设备上的Termux UNIX shell,它可以通过/system/bin/getprop命令获得,而每个值的含义都在Android中的Build.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中status-right的pane_title,如下所示:

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


首先,

Adb shell getprop >prop_list.txt

第二,

在prop_list.txt中找到您的设备名称以获得道具名称, 例如,我的设备名称是ro.oppo.market.name

最后,

Adb shell getprop ro.oppo.market.name

D:\winusr\adbl

λ adb shell getprop ro.oppo.market.name

成本R17

D:\winusr\adbl

λ


尝试了这些库,但没有按照我的期望工作,并给出了绝对错误的名称。

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

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