且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式获取 Android 手机型号,如何在 android 中以编程方式获取设备名称和型号?

更新时间:2023-01-27 12:49:16

在许多流行的设备上,设备的市场名称不可用.例如,在三星 Galaxy S6 上,Build.MODEL 的值可以是 SM-G920F"SM-G920I", 或 SM-G920W8".

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

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

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:

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

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;

  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 的示例:


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

结果:

HTC6525LVW

HTC One (M8)

HTC One (M8)