How to get real device model in Android?

蓝咒 提交于 2019-12-24 01:45:27

问题


For example, on my Xperia mini phone,

  • Build.MODEL returns 'st15i'
  • Build.MANUFACTURER returns 'Sony Ericsson'

but I want to get 'Sony Ericsson xperia mini' for this phone.

Is it possible?


回答1:


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

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;
  }
}



回答2:


ST15I is the model code for the XPeria mini. So maybe you shoud use Build.DEVICE, ord build a correspondance base for the various codes to their names.



来源:https://stackoverflow.com/questions/12091767/how-to-get-real-device-model-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!