1

I want to validate the version.I am using this code

String deviceVersion= Build.VERSION.RELEASE;
String limitVersion="4.0";

storing it in a variable For eg:deviceVersion is 2.3.6 i cannot convert it to the integer .How can i do this.

I want to check like this.

if(deviceVersion => limitVersion )
{
//do something
}else{
//do something
}
Deepak
  • 192
  • 1
  • 2
  • 18
  • Possible Duplicate http://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java – newuser Sep 23 '13 at 08:03

4 Answers4

3

Use the API level. This is one of the very first thing the Android Developer's site teaches you in their training docs, if you haven't had a chance to read it yet I highly recommend it. There's a ton of good information in there.

if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
    // Is Honeycomb or newer
}
Jon
  • 1,358
  • 8
  • 13
3

You can get SDK version as integer by

int version=Build.VERSION.SDK_INT;

then you can compare it to some value. Like

if(version>=Build.VERSION_CODES.GINGERBREAD_MR1){
    do_something();
}
else{
    do_something_else();
}

You can get other version code by

Build.VERSION_CODES.*

Eclipse IDE will help you to find what you are looking for.

Edit: Fixed for the version ID you're looking for

mgokgoz
  • 186
  • 1
  • 9
1

This will basically take the first integer before the first dot. Make sure to do appropriate checks if the script is empty or if a dot even exists first.

String versionInt = deviceVersion.split("\\.")[0];
Dzhuneyt
  • 7,039
  • 11
  • 56
  • 108
  • 1
    Why the downvote? I know that Android provides a native way of getting the version code, but since that solution was answered in a few answers already - I decided to provide a different perspective - of using native Java functionality. Maybe that's what the OP needed. Who said he needed to get the Android build version? – Dzhuneyt Sep 23 '13 at 19:58
  • Yes, thank you! :D – sanevys May 18 '21 at 08:57
0

You can use this:

android.os.Build.VERSION

to get the version, and then you can use it to implement your code..

Teo
  • 2,994
  • 2
  • 24
  • 56