@SuppressLint("NewApi")和@TargetApi()的区别章标题
来源:互联网 发布:dbxpa调试软件 编辑:程序博客网 时间:2024/06/05 15:13
转自: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错误,所以在方法中还要判断版本做不同的操作,比如:
[java] view plaincopy 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()的区别
- 尚学堂nginx 踩过得坑
- APP校验 Token的生成流程
- uboot中挂载U盘,利用FAT文件系统读写U盘文件
- MySQL创建全文索引
- Retrofit源码解析(四)---ServiceMethod相关分析
- @SuppressLint("NewApi")和@TargetApi()的区别章标题
- 【Web前端开发】Vue+AJAX+Bootstrap-table导致失效问题
- 如何从ubuntu14.04下硬盘安装ubuntu
- IplImage与Mat的相互转换 内存泄漏
- GRPC原理解析
- vue耦合程度是什么意思
- 隧道接口工具airtun-ng
- APP怎样接入支付宝或微信支付
- json字符串插入节点或者覆盖节点