Getting device os version in Android programmatically

How can I get the current android device version (1.5, 1.6, 2.0, etc.) programmatically? i.e I have installed my apk on an Android 2.2 device. I need to get the device version (android emulator version in which my application is currently running ).


Something like these:

String myVersion = android.os.Build.VERSION.RELEASE; // e.g. myVersion := "1.6"
int sdkVersion = android.os.Build.VERSION.SDK_INT; // e.g. sdkVersion := 8; 

You can retrieve all SDK codes from Build.VERSION_CODES


int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
    // Do something for lollipop and above versions
} else{
    // do something for phones running an SDK before lollipop
}

By this code we can execute separate code for Pre Lollipop devices.


If you want to see the firmware version number then you can use

Build.VERSION.SDK_INT

But if you want firmware version name then you should use something like this

Build.VERSION_CODES

Try This,

    String deviceOs = android.os.Build.VERSION;

I hope this will help.