11

My app supports minSdkVersion=10 and targeting 16. I want to call methods specific to API level >= 14 if a specific device supports them. I could check running OS version at runtime and whether call or not higher API methods but when I specify min SDK version, methods that exist only in versions higher than 10 are not visible. Is there any way to use higher API methods than minSdkVersion?

Maxim
  • 4,152
  • 8
  • 50
  • 77

3 Answers3

12

You can test the device's API with this:

if(android.os.Build.VERSION.SDK_INT >= 14) {
    // Do something fancy
}
else {
    // Do something regular
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • This is what I mentioned. I can check the OS version but methods specific only for SDK higher then 10 are inaccessible. //Do something will fail build, because method that exists only in SDK 14 or higher is undefined for SDK 10. – Maxim Jul 31 '12 at 22:42
  • 2
    Ok, here's the blog entry from Android discussing the concept in detail: [How to have your (Cup)cake and eat it too](http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html) – Sam Jul 31 '12 at 23:07
  • Up vote. Blog post gave me idea why higher API methods are invisible in my code. Answer is simply target highest SDK in project properties. – Maxim Aug 02 '12 at 22:18
5

In addition of checking the current version you should also add @SuppressLint("NewApi")to your method so the compiler want yell about it.

shem
  • 4,686
  • 2
  • 32
  • 43
3

Methods from higher API are invisible and inaccessible because project's target SDK is lower than SDK which methods are going to be used. For example: if you want to use methods from API 14 Android project target SDK should be at least 14 or even better the latest (currently 16). That is kind of obvious but I missed it. After that the solution Sam gave a reference to is in use.

Maxim
  • 4,152
  • 8
  • 50
  • 77