Android @SuppressLint("NewApi")和@TargetApi()的区别
来源:互联网 发布:java获取类名 编辑:程序博客网 时间:2024/06/05 04:23
原址: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()的区别
- axure 原型设计20条军规
- 快速将List对象、Map对象或者普通普通对象转换成json字符串
- 第十六周项目1-小玩文件(2)
- UIAlertController的用法
- swift2.0 学习笔记Seventeen
- Android @SuppressLint("NewApi")和@TargetApi()的区别
- Jni
- android 在命令行中查看SQLite数据库数据
- 坐标系(下) convertToNodeSpace和convertToWorldSpace
- 【NOI 2010】【BZOJ 2005】【JZOJ 2225】能量采集
- Android开发之RecyclerView的使用全解
- c#中的get函数和set函数的用法
- css bug
- JAVA常用集合框架源码解析(基于1.8)开题篇