我想知道是否有办法在 Android 中以编程方式读取电话模型。
我想得到一个像 HTC Dream、Milestone、Sapphire 或其他什么的字符串......
我想知道是否有办法在 Android 中以编程方式读取电话模型。
我想得到一个像 HTC Dream、Milestone、Sapphire 或其他什么的字符串......
我使用以下代码来获取完整的设备名称。它获取模型和制造商字符串并将它们连接起来,除非模型字符串已经包含制造商名称(在某些手机上它包含):
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
...</p>
在许多流行的设备上,该设备的市场名称不可用。例如,在三星 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
宏达一 (M8)
是的:Build.MODEL。
对于寻找Build
此处完整属性列表的人来说,这是 Sony 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 使用“评估表达式”对话框在调试模式下轻松列出设备的这些属性:
android.os.Build::class.java.fields.map { "Build.${it.name} = ${it.get(it.name)}"}.joinToString("\n")
实际上,这不是 100% 正确的。这可以给你模型(有时是数字)。
将为您提供手机的制造商(您请求的 HTC 部分):
Build.MANUFACTURER
对于产品名称:
Build.PRODUCT
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);
}
显然您需要使用来自 Google 的列表https://support.google.com/googleplay/answer/1727131
API 不会返回我期望的任何内容或设置中的任何内容。对于我的摩托罗拉 X,这就是我得到的
Build.MODEL = "XT1053"
Build.BRAND = "motorola"
Build.PRODUCT = "ghost"
转到上面提到的页面“ghost”映射到 Moto X。看起来这可能更简单一点......
当您要检索制造商、设备名称和/或型号时,可以使用以下字符串:
String manufacturer = Build.MANUFACTURER;
String brand = Build.BRAND;
String product = Build.PRODUCT;
String model = Build.MODEL;
您可以使用以下代码获取设备的品牌名称和品牌型号。
String brand = Build.BRAND; // for getting BrandName
String model = Build.MODEL; // for getting Model of the device
Kotlin 短版:
import android.os.Build.MANUFACTURER
import android.os.Build.MODEL
fun getDeviceName(): String =
if (MODEL.startsWith(MANUFACTURER, ignoreCase = true)) {
MODEL
} else {
"$MANUFACTURER $MODEL"
}.capitalize(Locale.ROOT)
Build.DEVICE // The name of the industrial design.
Build.DEVICE为某些设备提供人类可读的名称,而不是Build.MODEL
Build.DEVICE = OnePlus6
Build.MANUFACTURER = OnePlus
Build.MODEL = ONEPLUS A6003
这是我的代码,获取制造商、品牌名称、操作系统版本并支持 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
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)
}
}
稍微更改了 Idolons 代码。这将在获取设备型号时大写单词。
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();
}
}
您可以从
蓝牙适配器
如果手机不支持蓝牙,那么您必须从
android.os.Build 类
这是获取电话设备名称的示例代码。
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;
}
您可以尝试以下函数,并以字符串格式返回您的 phoneModel 名称。
public String phoneModel() {
return Build.MODEL;
}