如何在Android中获得真实的设备模型?

如何在Android中获得真实的设备模型?,android,device,Android,Device,例如,在我的Xperia迷你手机上 返回“st15i” 返回“索尼爱立信” 但我想买这款手机的“索尼爱立信xperia mini” 可能吗?ST15I是XPeria mini的型号代码。因此,也许您应该使用Build.DEVICE,或者为各种代码与其名称建立一个对应的基础。对于特定的手机(以及许多其他SonyEricsson手机),您只能通过读取您提到的系统属性来获取真实的设备名称:ro.semc.product.model 由于android.os.SystemProperties类对公共

例如,在我的Xperia迷你手机上

  • 返回“st15i”
  • 返回“索尼爱立信”
但我想买这款手机的“索尼爱立信xperia mini”

可能吗?

ST15I是XPeria mini的型号代码。因此,也许您应该使用
Build.DEVICE
,或者为各种代码与其名称建立一个对应的基础。

对于特定的手机(以及许多其他SonyEricsson手机),您只能通过读取您提到的系统属性来获取真实的设备名称:
ro.semc.product.model

由于
android.os.SystemProperties
类对公共API是隐藏的,因此您需要使用反射(或exec
getprop ro.semc.product.model
命令和):

公共字符串getSonyEricssonDeviceName(){
字符串模型=getSystemProperty(“ro.semc.product.model”);
返回值(model==null)?“”:model;
}
私有字符串getSystemProperty(字符串propName){
类clsSystemProperties=tryClassForName(“android.os.SystemProperties”);
方法mtdGet=tryGetMethod(clsSystemProperties,“get”,String.class);
返回tryInvoke(mtdGet,null,propName);
}
私有类tryClassForName(字符串类名称){
试一试{
返回Class.forName(className);
}catch(classnotfounde异常){
返回null;
}
}
私有方法tryGetMethod(类cls、字符串名称、类…参数类型){
试一试{
返回cls.getDeclaredMethod(名称、参数类型);
}捕获(例外e){
返回null;
}
}
@抑制警告(“未选中”)
私有T tryInvoke(方法m、对象、对象…args){
试一试{
返回(T)m.invoke(对象,args);
}捕获(调用TargetException e){
抛出新的运行时异常(e);
}捕获(例外e){
返回null;
}
}

谢谢,Build.DEVICE与Build.MODEL相同并返回'st15i'。请检查此项,您可以使用
Build.MANUFACTURER
查看型号名称,使用
Build.PRODUCT
查看产品名称谢谢,Build.PRODUCT返回'st15i_1249-8388',这不是我想要的。我检查了手机中的build.prop文件,“xperia mini”位于“ro.semc.product.model”下。
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;
  }
}