@TargetApi and @SuppressLint
来源:互联网 发布:免费点播软件 编辑:程序博客网 时间:2024/04/30 12:38
@TargetApi
and @SuppressLint
have the same core effect: they suppress the Lint error.
The difference is that with @TargetApi
, you declare, via the parameter, what API level you have addressed in your code, so that the error can pop up again if you later modify the method to try referencing something newer than the API level cited in @TargetApi
.
For example, suppose that, instead of blocking the StrictMode
complaints about your networking bug, you were trying to work around the issue of AsyncTask
being serialized on newer versions of Android. You have a method like this in your code to opt into the thread pool on newer devices and use the default multithread behavior on older devices:
@TargetApi(11) static public <T> void executeAsyncTask(AsyncTask<T, ?, ?> task, T... params) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); } else { task.execute(params); } }
Having @TargetApi(11)
means that if Lint detects that I am using something newer than my android:minSdkVersion
, but up to API Level 11, Lint will not complain. In this case, that works. If, however, I modified this method to reference something that wasn't added until API Level 14, then the Lint error would appear again, because my @TargetApi(11)
annotation says that I only fixed the code to work on API Level 11 and below, not API Level 14 and below.
Using @SuppressLint('NewApi')
, I would lose the Lint error for any API level, regardless of what my code references and what my code is set up to handle.
Hence, @TargetApi
is the preferred annotation, as it allows you to tell the build tools "OK, I fixed this category of problems" in a more fine-grained fashion.
- @TargetApi and @SuppressLint
- Android中的@TargetApi and @SuppressLint
- @SuppressLint or @TargetApi
- @SuppressWarinings, @TargetApi, @SuppressLint, @ViewInject
- android TargetApi 和 SuppressLint
- TargetAPI和SuppressLint
- @TargetApi() 与@SuppressLint("NewApi")
- What is better: @SuppressLint or @TargetApi?
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi(number)使用比较
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- @SuppressLint("NewApi")和@TargetApi()的区别
- ubuntu下的NDK配置
- CCProgressTimer进度条比LoadingBar靠谱
- 详解数据中心交换机的关键参数
- nginx防盗链
- AngularJS学习(二)——表达式,ng-bind,ng-app,ng-init,ng-repeat,ng-model
- @TargetApi and @SuppressLint
- hexo相关问题
- 【c语言】char无符号超范围的结果
- linux 安装unzip zip
- Spark MLlib SVM算法
- apache 集成多个tomcat的方法
- 《围城》读后感
- Linux 原始套接字发送接收示例
- Android Volley解析(一)之GET、POST请求篇