@SuppressLint("NewApi")和@TargetApi()的区别
来源:互联网 发布:tor网络 原理 编辑:程序博客网 时间:2024/05/18 01:27
在Android代码中,我们有时会使用比我们在AndroidManifest中设置的android:minSdkVersion版本更高的方法,此时编译器会提示警告,解决方法是在方法上加上@SuppressLint("NewApi")或者@TargetApi()。
那他们之间有什么区别呢,很简单,
@SuppressLint("NewApi")屏蔽一切新api中才能使用的方法报的android lint错误
@TargetApi() 只屏蔽某一新api中才能使用的方法报的android lint错误
举个例子,某个方法中使用了api9新加入的方法,而项目设置的android:minSdkVersion=8,此时在方法上加@SuppressLint("NewApi")和@TargetApi(Build.VERSION_CODES.GINGERBREAD)都可以,以上是通用的情况。
而当你在此方法中又引用了一个api11才加入的方法时,@TargetApi(Build.VERSION_CODES.GINGERBREAD)注解的方法又报错了,而@SuppressLint("NewApi")不会报错,这就是区别。
当然,不管你使用了哪个注解,作用仅仅是屏蔽android lint错误,所以在方法中还要判断版本做不同的操作,比如:
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
- //
- } else {// Pre GINGERBREAD
- //
- }
0 0
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- JMS两种模型,布上apache-activemq服务器JMS(四)
- 常用socket函数详解
- 设计模式(9)——装饰者模式(Decorator Pattern)
- building workspace js validation
- VS发布出现 错误 X 未能将文件 复制到 未能找到文件
- @SuppressLint("NewApi")和@TargetApi()的区别
- Java中静态成员
- hibernate discriminator
- umeng分享到QQ,分享成功,回调失败。
- js 三元表达式
- 通过javascript 获取当前地理位置 所在城市
- Java中的静态内部类与非静态内部类的区别
- 设计模式(10)——组合模式(Composite Pattern)
- Centos 6.7 下安装git 各种累心折腾