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.
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.
if (Build.VERSION.SDK_INT >= x ) {}
x is the api number, Honeycomb is 11 you can find api numbers here: Platform versions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With