0

How do you get the sdk version inside a class? I want to compare the SDK version to a number and do something if its a certain verion.

yoshi24
  • 2,947
  • 7
  • 41
  • 61

2 Answers2

1

You can use Build.VERSION.SDK_INT to get an int representing the SDK version, and compare it to a given version code : Build.VERSION_CODES.* (ECLAIR, FROYO, etc...).

This is available from anywhere in your code, and works starting at API Level 4. Prior to API Level 4 (if you plan on targeting really old devices), you can use Build.VERSION_SDK, which returns pretty much the same information but in a String (then use Integer.parseInt to parse it...).

Gregory
  • 4,174
  • 1
  • 23
  • 21
0

Build.VERSION.RELEASE can give you the user-visible version string (i.e 1.5, 1.6, 2.0), while Build.VERSION.SDK_INT can give you a value from Build.VERSION_CODES that would be better to use if you want to compare against it (programatically).

But Build.VERSION.SDK_INT is only available on Android 1.6 and newer. Build.VERSION.SDK will work on all Android releases, including 1.5. However, once you elect to drop 1.5 support, switching to SDK_INT is a good idea.

Hein
  • 2,602
  • 20
  • 32