API call with a higher level API than the minimum requirement

I wrote most of the application, just fine, with a minimum API level set to 7. I want to name one API from level 8. Users with lower versions of android will survive without this "extra feature".

Currently I have added @SuppressLint("NewApi") so that my code can work; and I am testing it on API 14. Everything is working fine.

Interestingly, the application will behave on API 7 devices. Will this line be ignored? Will my application crash? Will the application be filtered by Google Play so that they cannot install?

I would like this single line to be ignored on lower devices.

+2
source share
3 answers

Will this line be ignored?

Not.

Can my application crash?

Spectactularly .: -)

Will the application be filtered on Google Play so that they cannot install?

Not.

I would like this single line to be ignored on lower devices.

You have two problems:

  • @SuppressLint("NewApi") was the wrong quick fix choice

  • You have not added code to avoid this line on older devices

Use @TargetApi(...) instead of @SuppressLint("NewApi") , where ... is the name (for example, FROYO ) or the number (for example, 8 ) of the code that your method refers to.

But before you do this, wrap your infringing lines in a check to see if they should run 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 eliminated. Your @TargetApi annotation @TargetApi cause Lint to stop yelling at you about referencing a class or method too new.

+11
source

Better wrap it around

 import android.os.Build; if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR_MR1) { // do what you want } 
+3
source

Your application will crash if you do not have an if statement to check the API, to ignore it if the API == 7. I worked with devices from 4.0 to 4 and did some tests using Airplane mode, which in API 4.1 and below is in Settings.System , but in 4.2 it is under Settings.Global . If I tried to name the wrong application, it would crash.

+1
source

Source: https://habr.com/ru/post/1204626/


All Articles