如何在Android中获取真实的设备模型? [英] How to get real device model in Android?
本文介绍了如何在Android中获取真实的设备模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,在我的Xperia迷你手机上,
For example, on my Xperia mini phone,
- Build.MODEL 返回'st15i'
- Build.MANUFACTURER 返回索尼爱立信
- Build.MODEL returns 'st15i'
- Build.MANUFACTURER returns 'Sony Ericsson'
,但我想为此手机获得'Sony Ericsson xperia mini'。
but I want to get 'Sony Ericsson xperia mini' for this phone.
有可能吗?
推荐答案
针对该特定手机(也许还有许多其他索尼爱立信手机)您只能通过读取您提到的系统属性来获得真实的设备名称: ro.semc.product.model
For that particular phone (and perhaps for many others SonyEricsson phones) you can get the real device name only by reading system property you have mentioned: ro.semc.product.model
由于 android.os.SystemProperties
类已从公共API中隐藏,因此您需要使用反射(或exec getprop ro.semc.product.model
命令和获取其输出):
Since android.os.SystemProperties
class is hidden from public API you will need to use a reflection (or exec getprop ro.semc.product.model
command and grab its output):
public String getSonyEricssonDeviceName() {
String model = getSystemProperty("ro.semc.product.model");
return (model == null) ? "" : model;
}
private String getSystemProperty(String propName) {
Class<?> clsSystemProperties = tryClassForName("android.os.SystemProperties");
Method mtdGet = tryGetMethod(clsSystemProperties, "get", String.class);
return tryInvoke(mtdGet, null, propName);
}
private Class<?> tryClassForName(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return null;
}
}
private Method tryGetMethod(Class<?> cls, String name, Class<?>... parameterTypes) {
try {
return cls.getDeclaredMethod(name, parameterTypes);
} catch (Exception e) {
return null;
}
}
@SuppressWarnings("unchecked")
private <T> T tryInvoke(Method m, Object object, Object... args) {
try {
return (T) m.invoke(object, args);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (Exception e) {
return null;
}
}
这篇关于如何在Android中获取真实的设备模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文