Calling API from higher API level than the minimum requirement

前端 未结 3 1443
清歌不尽
清歌不尽 2021-01-23 05:22

I wrote most of an app just fine with min API level set to 7. I want to call a single API from level 8. Users with lower versions of android will survive without this \"extra fe

相关标签:
3条回答
  • 2021-01-23 06:03

    It is better to wrap this around

    import android.os.Build;
    
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR_MR1) {
        // do what you want
    }
    
    0 讨论(0)
  • 2021-01-23 06:05

    Would this one line be ignored?

    No.

    Would my app crash?

    Spectactularly. :-)

    Would the app be filtered by Google Play so they can't install?

    No.

    I'd like to have this single line ignore on lower devices.

    You have two problems:

    1. @SuppressLint("NewApi") was the wrong quick-fix choice

    2. You didn't add any code to avoid this line on older devices

    Use @TargetApi(...) instead of @SuppressLint("NewApi"), where ... is the name (e.g., FROYO) or number (e.g., 8) of the code that your method is referencing.

    But before you do that, wrap your offending lines in a check to see if they should be executed on this device:

    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.FROYO) {
      // then execute your code that requires API Level 8
    }
    // optional else block if you have some workaround for API Level 7
    

    Your if check will cause your line to be avoided. Your @TargetApi annotation will cause Lint to stop yelling at you about referencing a too-new class or method.

    0 讨论(0)
  • 2021-01-23 06:05

    Your app will crash if you do not have an if statement to check API in order to ignore it if API == 7. I was working with 4.0 to 4.2 devices and did some testing with the Airplane mode, which in API 4.1 and below is found in Settings.System but in 4.2 it is under Settings.Global. If I tried calling the wrong one the app would crash.

    0 讨论(0)
提交回复
热议问题