@SuppressLint("NewApi")和@TargetApi()的区别
来源:互联网 发布:网络舆情汇报 编辑:程序博客网 时间:2024/05/01 01:45
在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()的区别
- linux 文件及命令(五)
- springmvc 原理解析
- 关于今天在用spring字符集过滤器遇到的问题
- jQuery 之缓存
- Hbuilder/H5 功能汇总
- @SuppressLint("NewApi")和@TargetApi()的区别
- webrtc测试服务器编译和搭建
- MVVM初识
- Zigbee性能分析
- PHP+mysql链接
- jQuery 放大镜(面向过程)
- RecyclerView的使用教程1
- Linux-C基础知识学习:C语言作业-把100~200之间的不能被3整除的数输出
- <link>标签中的rel="sheetstyle"含义