Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check HoneyComb or higher is running, and accordingly call a method for that Version?

Tags:

android

For Android Versions 3.0 and higher, I want to call a certain method. Is there a way to check if a certain method is available in the running Android Version?

To be more precise, my MinSDK is 7 (Android 2.1), TargetSDK is 8 (Android 2.2) and I need to test if HoneyComb Android 3.0 or higher is running. Depending on that, how can I call that HoneyComb method?

The second part of the question arises, because simply calling that HoneyComb method, will not compile, as I am building against 2.2.

like image 566
mrd Avatar asked Mar 31 '12 21:03

mrd


2 Answers

To be more precise, my MinSDK is 7 (Android 2.1), TargetSDK is 8 (Android 2.2) and I need to test if HoneyComb Android 3.0 or higher is running. Depending on that, how can I call that HoneyComb method?

Step #1: Set your build target to the highest API level you wish to call directly and therefore compile against. Your build target (e.g., compileSdkVersion in Android Studio, Project > Properties > Android in Eclipse) is not related to your android:targetSdkVersion.

Step #2: As the other answers have indicated, you can then conditionally call methods within a guard block:

if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
  // call something for API Level 11+
}

The second part of the question arises, because simply calling that HoneyComb method, will not compile, as I am building against 2.2.

You need to change your build target to be API Level 11 or higher if you wish to directly call API Level 11 or higher methods.

like image 118
CommonsWare Avatar answered Oct 04 '22 02:10

CommonsWare


if (Build.VERSION.SDK_INT >= x ) {}

x is the api number, Honeycomb is 11 you can find api numbers here: Platform versions

like image 33
Blackguard Avatar answered Oct 04 '22 03:10

Blackguard