我想知道是否有一种方法可以在Android中以编程方式读取Phone Model。
我想要一个像HTC Dream,Milestone,Sapphire或其他任何东西的字符串。
我想知道是否有一种方法可以在Android中以编程方式读取Phone Model。
我想要一个像HTC Dream,Milestone,Sapphire或其他任何东西的字符串。
Answers:
在许多流行的设备上,设备的市场名称不可用。例如,在三星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)
getDeviceName()
?如果这样不能为您提供所需的结果,那么我就没有更好的解决方案了。
我使用以下代码来获取完整的设备名称。除非模型字符串已经包含制造商名称(在某些手机上确实如此),否则它将获取模型和制造商字符串并将其串联起来:
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
…
是:Build.MODEL。
其实那不是100%正确的。这样可以给您Model(有时是数字)。
将为您提供电话的制造商(您要求的HTC部分):
Build.MANUFACTURER
产品名称:
Build.PRODUCT
String name = Build.MANUFACTURER + " - " + Build.MODEL
是最有用的组合。Build.PRODUCT有时使用一个意外的名称。例如,在Galaxy Nexus上,它返回“ takgu”。另一方面,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")
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);
}
您可以使用以下代码获取设备的品牌名称和品牌型号。
String brand = Build.BRAND; // for getting BrandName
String model = Build.MODEL; // for getting Model of the device
这是我的代码,以获取制造商,品牌名称,操作系统版本和支持的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)
}
}
capitalize
stackoverflow.com/a/59158474/1446466
更改了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();
}
}
Build.DEVICE // The name of the industrial design.
Build.DEVICE提供某些设备的可读名称,Build.MODEL
Build.DEVICE = OnePlus6
Build.MANUFACTURER = OnePlus
Build.MODEL = ONEPLUS A6003
您可以从
蓝牙适配器
如果手机不支持蓝牙,则必须从
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;
}