Ali.M Ali.M - 4 months ago 22
Android Question

How to get real device model in Android?

For example, on my Xperia mini phone,



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

Is it possible?

Answer

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