Calling API from higher API level than the minimum requirement

前端 未结 3 1453
清歌不尽
清歌不尽 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: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.

提交回复
热议问题