@SuppressLint("NewApi")和@TargetApi()的区别
来源:互联网 发布:数据存储方案 编辑:程序博客网 时间:2024/05/10 21:01
转自:http://blog.csdn.net/wbshuang09/article/details/44920549
在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()的区别
- 1013 数素数
- Python 3 邮件的接收(IMAP)
- CentOS 6 无法上网 问题解决方案
- 集合(二)Collection的实现List和Set
- IOS 10 适配系列_2_ IOS10 适配汇总:ATS、隐私数据、UserNotifications、UICollectionView汇总
- @SuppressLint("NewApi")和@TargetApi()的区别
- Ubuntu 14.04 LTS 下升级 gcc 到 gcc-4.9、gcc-5 版本
- EventUtil.js 开发常用工具类
- POJ 1286 2409 Polya计数
- Python正则表达式指南
- Java多线程基础
- TQ210——S5PV210 uboot顶层config.mk分析
- 5个可以帮你优化App的优秀网站
- TCP的握手