@SuppressLint("NewApi")和@TargetApi()的区别
2016-08-22 09:28
513 查看
在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
plain copy
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
//
} else {// Pre GINGERBREAD
//
}
那他们之间有什么区别呢,很简单,
@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
plain copy
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
//
} else {// Pre GINGERBREAD
//
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201701/9cc493f1e15b23d0f4eaea0d0f8b35d0.png)
相关文章推荐
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别章标题
- @SuppressLint("NewApi")和@TargetApi()的区别
- android 基础 @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()的区别
- Android @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- Android中Build类以及@SuppressLint("NewApi")和@TargetApi()区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- 我的Android进阶之旅------>Android中高低API版本兼容使用@TargetApi或者@SuppressLint("NewApi")
- 在低版本的SDK里使用高版本函数@SuppressLint("NewApi") or @TargetApi?