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

前端 未结 2 431
-上瘾入骨i
-上瘾入骨i 2021-02-08 01:02

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

2条回答
  •  被撕碎了的回忆
    2021-02-08 01:14

    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.

提交回复
热议问题