以编程方式获取Android手机模型
我想知道是否有一种方式在android中以编程方式阅读手机模型。
我想获得像HTC Dream,Milestone,Sapphire或其他什么的string…
谢谢 :)
在许多stream行的设备上,设备的市场名称不可用。 例如,在Samsung Galaxy S6上, Build.MODEL
的值可以是"SM-G920F"
, "SM-G920I"
或"SM-G920W8"
。
我创build了一个小型图书馆,以获得设备的市场(消费者友好)名称。 它获得超过10,000个设备的正确名称,并不断更新。 如果你想使用我的图书馆,请点击下面的链接:
Github上的AndroidDeviceNames库
如果你不想使用上面的库,那么这是获得消费者友好设备名称的最佳解决scheme:
/** 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)
我使用下面的代码来获取完整的设备名称。 它得到模型和制造商的string,并连接它们,除非模型string已经包含制造商名称(在一些手机上):
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); } }
以下是我从用户处获得的一些设备名称的示例:
三星GT-S5830L
摩托罗拉MB860
索尼爱立信LT18i
LGE LG-P500
HTC Desire V
HTC野火S A510e
…
是的: Build.MODEL 。
其实这不是100%正确的。 这可以给你模型(有时数字)。
将得到您的手机制造商(您的要求HTC部分):
Build.MANUFACTURER
对于产品名称:
Build.PRODUCT
显然,您需要使用Google的列表: https://support.google.com/googleplay/answer/1727131
API不会返回任何我期望的或任何设置。 对于我的摩托罗拉X这是我得到的
Build.MODEL = "XT1053" Build.BRAND = "motorola" Build.PRODUCT = "ghost"
去上面提到的“鬼”映射到Moto X.似乎这可能是一个简单的…
您可以使用以下代码获取设备的品牌名称和品牌型号。
String brand = Build.BRAND; // for getting BrandName String model = Build.MODEL; // for getting Model of the device
当您想检索制造商,设备名称和/或型号时,以下string全部使用:
String manufacturer = Build.MANUFACTURER; String brand = Build.BRAND; String product = Build.PRODUCT; String model = Build.MODEL;
索尼Z1 Compact的一个例子是谁在寻找完整的Build
属性列表:
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使用“评估expression式”对话框以debugging模式轻松地列出设备的这些属性:
android.os.Build::class.java.fields.map { "Build.${it.name} = ${it.get(it.name)}"}.joinToString("\n")
String deviceName = android.os.Build.MODEL; //返回模型名称
String deviceManufacturer = android.os.Build.MANUFACTURER; //返回制造商
这里是我的代码,要获得制造商,品牌名称,Os版本和支持API级别
String manufacturer = Build.MANUFACTURER; String model = Build.MODEL + " " + android.os.Build.BRAND +" (" + android.os.Build.VERSION.RELEASE+")" + " API-" + android.os.Build.VERSION.SDK_INT; if (model.startsWith(manufacturer)) { return capitalize(model); } else { return capitalize(manufacturer) + " " + model; }
输出:
System.out: button press on device name = Lava Alfa L iris(5.0) API-21
改变了偶像代码一点。 当获取设备模型时,这将大写字母。
public static String getDeviceName() { final String manufacturer = Build.MANUFACTURER, model = Build.MODEL; return model.startsWith(manufacturer) ? capitalizePhrase(model) : capitalizePhrase(manufacturer) + " " + model; } private static String capitalizePhrase(String s) { if (s == null || s.length() == 0) return s; else { StringBuilder phrase = new StringBuilder(); boolean next = true; for (char c : s.toCharArray()) { if (next && Character.isLetter(c) || Character.isWhitespace(c)) next = Character.isWhitespace(c = Character.toUpperCase(c)); phrase.append(c); } return phrase.toString(); } }