How to get real device model in Android?

2019-07-20 16:44发布

问题:

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.