Retrieving Android API Version Programmatically

Android API Version programmatically

You can get it by calling Build.VERSION.SDK.

From 1.6 on, you should use Build.VERSION.SDK_INT instead

because Build.VERSION.SDK is deprecated.

How to find the Android version name programmatically?

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!

How can I check the system version of Android?

Check android.os.Build.VERSION.

  • CODENAME: The current development codename, or the string "REL" if this is a release build.
  • INCREMENTAL: The internal value used by the underlying source control to represent this build.
  • RELEASE: The user-visible version string.

Retrieving API version programmatically using Context

You can use Build.VERSION.RELEASE to get Android version as displayed in About Phone information

How to get the Android API version at runtime from Delphi Rio?

It can be done this way:

uses
Androidapi.JNI.Os;

...

if TJBuild_VERSION.JavaClass.SDK_INT > SomeValue then

Where the value of SomeValue is the API level you want to check against.



Related Topics



Leave a reply



Submit