Skip to content Skip to sidebar Skip to footer

Calling Api From Higher Api Level Than The Minimum Requirement

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 featur

Solution 1:

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.

Solution 2:

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
}

Solution 3:

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.

Post a Comment for "Calling Api From Higher Api Level Than The Minimum Requirement"