我想知道是否有办法在Android中以编程方式阅读手机模型。
我想得到一个像HTC Dream,Milestone,Sapphire或其他任何东西的字符串......
答案 0 :(得分:403)
我使用以下代码获取完整的设备名称。它获取模型和制造商字符串并连接它们,除非模型字符串已包含制造商名称(在某些手机上):
public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) {
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 Wildfire S A510e
...
答案 1 :(得分:275)
在许多流行的设备上,设备的市场名称不可用。例如,在三星Galaxy S6上,Build.MODEL
的值可以是"SM-G920F"
,"SM-G920I"
或"SM-G920W8"
。
我创建了一个小型库,可以获得设备的市场(消费者友好型)名称。它为超过10,000个设备获取正确的名称,并不断更新。如果您想使用我的库,请单击以下链接:
如果您不想使用上面的库,那么这是获取消费者友好设备名称的最佳解决方案:
/** 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)
答案 2 :(得分:112)
是:Build.MODEL。
答案 3 :(得分:31)
实际上这不是100%正确。这可以给你模型(有时候数字) 将为您提供手机制造商(您的要求的HTC部分):
Build.MANUFACTURER
对于产品名称:
Build.PRODUCT
答案 4 :(得分:11)
String deviceName = android.os.Build.MODEL; // returns model name
String deviceManufacturer = android.os.Build.MANUFACTURER; // returns manufacturer
使用以下方法获取模型&制造商以编程方式
public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) {
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);
}
答案 5 :(得分:8)
当您想要检索制造商,设备名称和/或型号时,以下字符串都是有用的:
String manufacturer = Build.MANUFACTURER;
String brand = Build.BRAND;
String product = Build.PRODUCT;
String model = Build.MODEL;
答案 6 :(得分:8)
显然,您需要使用Google上的https://support.google.com/googleplay/answer/1727131
列表API不会返回我期望的任何内容或设置中的任何内容。对于我的摩托罗拉X,这就是我得到的
Build.MODEL = "XT1053"
Build.BRAND = "motorola"
Build.PRODUCT = "ghost"
转到上面提到的page“幽灵”地图到Moto X.似乎这可能有点简单......
答案 7 :(得分:7)
您可以使用以下代码获取品牌名称和 该设备的品牌型号。
String brand = Build.BRAND; // for getting BrandName
String model = Build.MODEL; // for getting Model of the device
答案 8 :(得分:3)
这是我的代码,获取制造商,品牌名称,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
答案 9 :(得分:2)
Kotlin版
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)
}
}
答案 10 :(得分:1)
改变了偶像代码。这将在获取设备模型时大写单词。
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();
}
}
答案 11 :(得分:1)
您可以从
获取电话设备名称BluetoothAdapter
如果手机不支持蓝牙,则必须从
构建设备名称android.os.Build class
以下是获取电话设备名称的示例代码。
public String getPhoneDeviceName() {
String name=null;
// Try to take Bluetooth name
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) {
name = adapter.getName();
}
// If not found, use MODEL name
if (TextUtils.isEmpty(name)) {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
name = model;
} else {
name = manufacturer + " " + model;
}
}
return name;
}
答案 12 :(得分:1)
科特琳短版:
import android.os.Build.MANUFACTURER
import android.os.Build.MODEL
fun getDeviceName(): String =
(if (MODEL.startsWith(MANUFACTURER, ignoreCase = true)) {
MODEL
} else {
"$MANUFACTURER $MODEL"
}).capitalize()
答案 13 :(得分:1)
Build.DEVICE // The name of the industrial design.
Build.DEVICE给出某些设备的可读名称,而不是Build.MODEL
Build.DEVICE = OnePlus6
Build.MANUFACTURER = OnePlus
Build.MODEL = ONEPLUS A6003
答案 14 :(得分:-3)
您可以尝试以下功能,并以字符串格式返回您的phoneModel名称。
public String phoneModel() {
return Build.MODEL;
}