How to get SDK Version inside of a class?
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 certa开发者_Python百科in verion.
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...).
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.
精彩评论