我想知道是否有一种在Android中以编程方式阅读手机模型的方法。
我想要一个像HTC梦想,里程碑,蓝宝石或任何字符串…
我想知道是否有一种在Android中以编程方式阅读手机模型的方法。
我想要一个像HTC梦想,里程碑,蓝宝石或任何字符串…
当前回答
稍微改变了偶像的代码。这将在获取设备模型时大写单词。
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();
}
}
其他回答
这是我的代码,以获得制造商,品牌名称,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
在许多流行的设备上,该设备的市场名称是不可用的。例如,在三星Galaxy S6上,Build的值。MODEL可以是“SM-G920F”、“SM-G920I”或“SM-G920W8”。
我创建了一个小型库,用于获取设备的市场(消费者友好)名称。它为超过10,000个设备获得正确的名称,并不断更新。如果你想使用我的图书馆,请点击下面的链接:
Github上的AndroidDeviceNames库
如果你不想使用上面的库,那么这是获得一个消费者友好的设备名称的最佳解决方案:
/** 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 manufacturer = Build.MANUFACTURER;
String brand = Build.BRAND;
String product = Build.PRODUCT;
String model = Build.MODEL;
Build.DEVICE // The name of the industrial design.
构建。DEVICE为某些设备提供人类可读的名称。模型
Build.DEVICE = OnePlus6
Build.MANUFACTURER = OnePlus
Build.MODEL = ONEPLUS A6003
方法获取电话设备名称
BluetoothAdapter
如果手机不支持蓝牙,那么您必须从构造设备名称
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;
}