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

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


当前回答

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

其他回答

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

你可以使用:

来自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类的情况下获取设备名称(通过蓝牙)。

试试这段代码。你会得到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() {
    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;
}

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

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

android.os.Build.MODEL;

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