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 ).
Getting device os version in Android programmatically
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
What is recommended to use for representing os, Build.VERSION.RELEASE or Build.VERSION.SDK.INT? Both get the job done, but is there an industry standard or more popular choice? –
Homework
@Homework
Build.VERSION.SDK_INT
is good at comparisons between versions (less than/greater than, etc.) –
Supinator s described in the android documentation, the SDK level (integer) the phone is running is available in: android.os.Build.VERSION.SDK_INT; The enum corresponding to this int is in the android.os.Build.VERSION_CODES class. Code example: int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO){ // Do something for froyo and above versions } else{ // do something for phones running an SDK before froyo } –
Delitescent
Edit: This SDK_INT is available since Donut (android 1.6 / API4) so make sure your application is not retro-compatible with Cupcake (android 1.5 / API3) when you use it or your application will crash (thanks to Programmer Bruce for the precision). –
Delitescent
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.
Take a look at the Build.VERSION version class. Depending on what you want to do, you might want to use the RELEASE
or SDK_INT
attribute.
© 2022 - 2024 — McMap. All rights reserved.