我想知道是否有一种在Android中以编程方式阅读手机模型的方法。

我想要一个像HTC梦想,里程碑,蓝宝石或任何字符串…


当前回答

你可以尝试跟随函数,它会以字符串格式返回你的phoneModel名字。

public String phoneModel() {

    return Build.MODEL;
}

其他回答

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

  StringBuilder phrase = new StringBuilder();
  for (char c : arr) {
    if (capitalizeNext && Character.isLetter(c)) {
      phrase.append(Character.toUpperCase(c));
      capitalizeNext = false;
      continue;
    } else if (Character.isWhitespace(c)) {
      capitalizeNext = true;
    }
    phrase.append(c);
  }

  return phrase.toString();
}

以我的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)

芬兰湾的科特林版本

val Name: String by lazy {
    val manufacturer = Build.MANUFACTURER
    val model = Build.MODEL
    if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) {
        capitalize(model)
    } else {
        capitalize(manufacturer) + " " + model
    }
}

private fun capitalize(s: String?): String {
    if (s == null || s.isEmpty()) {
        return ""
    }
    val first = s[0]
    return if (Character.isUpperCase(first)) {
        s
    } else {
        Character.toUpperCase(first) + s.substring(1)
    }
}

对于谁谁寻找完整的属性列表构建这里是一个索尼Z1 Compact的例子:

Build.BOARD = MSM8974
Build.BOOTLOADER = s1
Build.BRAND = Sony
Build.CPU_ABI = armeabi-v7a
Build.CPU_ABI2 = armeabi
Build.DEVICE = D5503
Build.DISPLAY = 14.6.A.1.236
Build.FINGERPRINT = Sony/D5503/D5503:5.1.1/14.6.A.1.236/2031203XXX:user/release-keys
Build.HARDWARE = qcom
Build.HOST = BuildHost
Build.ID = 14.6.A.1.236
Build.IS_DEBUGGABLE = false
Build.MANUFACTURER = Sony
Build.MODEL = D5503
Build.PRODUCT = D5503
Build.RADIO = unknown
Build.SERIAL = CB5A1YGVMT
Build.SUPPORTED_32_BIT_ABIS = [Ljava.lang.String;@3dd90541
Build.SUPPORTED_64_BIT_ABIS = [Ljava.lang.String;@1da4fc3
Build.SUPPORTED_ABIS = [Ljava.lang.String;@525f635
Build.TAGS = release-keys
Build.TIME = 144792559XXXX
Build.TYPE = user
Build.UNKNOWN = unknown
Build.USER = BuildUser

你可以在调试模式下使用kotlin的“evaluate expression”对话框轻松列出这些属性:

android.os.Build::class.java.fields.map { "Build.${it.name} = ${it.get(it.name)}"}.joinToString("\n")

你可以尝试跟随函数,它会以字符串格式返回你的phoneModel名字。

public String phoneModel() {

    return Build.MODEL;
}
Build.DEVICE // The name of the industrial design.

构建。DEVICE为某些设备提供人类可读的名称。模型

Build.DEVICE = OnePlus6
Build.MANUFACTURER = OnePlus
Build.MODEL = ONEPLUS A6003