Getting device os version in Android programmatically
Asked Answered
F

5

46

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 ).

Festus answered 7/9, 2011 at 3:58 Comment(1)
See duplicate: #3093865Alphard
S
107

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

Supinator answered 7/9, 2011 at 4:6 Comment(4)
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
D
14
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.

Disendow answered 17/12, 2015 at 10:25 Comment(0)
R
5

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
Riba answered 19/3, 2015 at 12:31 Comment(0)
W
5

Try This,

    String deviceOs = android.os.Build.VERSION;

I hope this will help.

Worldlywise answered 21/4, 2017 at 9:34 Comment(0)
V
1

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.

Viridissa answered 7/9, 2011 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.