以编程方式获取 Android 手机型号,如何在 android 中以编程方式获取设备名称和型号? [英] Get Android Phone Model programmatically , How to get Device name and model programmatically in android?

查看:87
本文介绍了以编程方式获取 Android 手机型号,如何在 android 中以编程方式获取设备名称和型号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法在 Android 中以编程方式读取电话模型.

I would like to know if there is a way for reading the Phone Model programmatically in Android.

我想要一个字符串,例如 HTC Dream、Milestone、Sapphire 或其他任何东西...

I would like to get a string like HTC Dream, Milestone, Sapphire or whatever...

推荐答案

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

这篇关于以编程方式获取 Android 手机型号,如何在 android 中以编程方式获取设备名称和型号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆