从比最低要求更高的API级别调用API [英] Calling API from higher API level than the minimum requirement

查看:253
本文介绍了从比最低要求更高的API级别调用API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了大部分的应用程序只是罚款最小API级别设置为7。我想打电话从水平一个单一的API 8.用户以更低版本的Andr​​oid将生存没有这个额外功能的。

目前我加入 @燮pressLint(NewApi)让我的code可以运行;而我对API测试它14.一切正常。

我不知道该应用程序的行为在API 7的设备是什么。请问这一条线被忽略?请问我的应用程序崩溃?将应用程序通过谷歌进行过滤玩,使他们无法安装?

我想有这样一行忽略较低的设备。

解决方案
  

请问这一条线被忽略?

没有。

  

请问我的应用程序崩溃?

Spectactularly。 : - )

  

请问该应用程序可以通过谷歌过滤玩,使他们无法安装?

没有。

  

我想有这样一行忽略较低的设备。

您有两个问题:

  1. @燮pressLint(NewApi)是错误的速战速决的选择

  2. 您没有添加任何code,以避免此行旧设备

使用 @TargetApi(...),而不是 @燮pressLint(下称NewApi) ,其中 ... 的名称(例如,升级Froyo )或数字(例如, 8 的code,你的方法是引用)。

不过的的你这样做,你的包裹犯罪行的检查,看他们是否应该在此设备上执行的:

 如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.FROYO){
  //然后执行你的code,需要API级别8
}
//可选的else块,如果你有一些解决方法API级别7
 

如果检查将导致避免您的生产线。你的 @TargetApi 注释会导致皮棉停在你大喊大叫有关引用一个太新的类或方法。

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 feature".

Currently I added @SuppressLint("NewApi") so that my code can run; and I'm testing it on API 14. Everything works fine.

I wonder what the app will behave on API 7 devices. Would this one line be ignored? Would my app crash? Would the app be filtered by Google Play so they can't install?

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

解决方案

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.

这篇关于从比最低要求更高的API级别调用API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆