I write code for finding the Android version like this
String version=Build.VERSION.RELEASE;
by using this code I am get the version number but I want version name. how to get the version name?
As suggested earlier, reflection seems to be the key to this question. The StringBuilder and extra formatting is not required, it was added only to illustrate usage.
import java.lang.reflect.Field;
...
StringBuilder builder = new StringBuilder();
builder.append("android : ").append(Build.VERSION.RELEASE);
Field[] fields = Build.VERSION_CODES.class.getFields();
for (Field field : fields) {
String fieldName = field.getName();
int fieldValue = -1;
try {
fieldValue = field.getInt(new Object());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
if (fieldValue == Build.VERSION.SDK_INT) {
builder.append(" : ").append(fieldName).append(" : ");
builder.append("sdk=").append(fieldValue);
}
}
Log.d(LOG_TAG, "OS: " + builder.toString());
On my 4.1 emulator, I get this output:
D/MainActivity( 1551): OS: android : 4.1.1 : JELLY_BEAN : sdk=16
Enjoy!