更好的是:@SuppressLint或@TargetApi? [英] What is better: @SuppressLint or @TargetApi?

查看:108
本文介绍了更好的是:@SuppressLint或@TargetApi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有关于 StrictMode 的问题,并添加了基本上禁用 StrictModeHelper 的代码段。但是,Lint现在抱怨 setThreadPolicy(),并建议添加

I have issues in my app regarding StrictMode and added the code snippet that basically disables the StrictModeHelper. However, Lint complains about setThreadPolicy() now and proposes to either add

@SuppressLint 'NewApi'

@TargetApi(Build.VERSION_CODES.GINGERBREAD)

onCreate()视图的事件。

哪种方法是首选的?或者他们基本上是一样的?

Which method is prefered ..or are they basically doing the same?

推荐答案


我的应用程序中有关于StrictMode的问题,并添加了基本禁用StrictModeHelper的代码段

I have issues in my app regarding StrictMode and added the code snippet that basically disables the StrictModeHelper

请修复网络错误。


哪种方法是优先的?或者他们基本上是一样的?

Which method is prefered ..or are they basically doing the same?

@TargetApi @SuppressLint 具有相同的核心效果:它们会抑制Lint错误。

@TargetApi and @SuppressLint have the same core effect: they suppress the Lint error.

区别在于与 @TargetApi ,您通过参数声明您在代码中解决了什么API级别,以便如果以后修改该方法以尝试引用比中引用的API级别更新的内容,则可能会再次弹出错误。 @TargetApi

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.

例如,假设,而不是阻止 StrictMode 投诉关于您的网络错误,您正在努力解决在较新版本的Android上序列化 AsyncTask 的问题。您的代码中有一个这样的方法可以选择在较新设备上使用线程池,并在旧设备上使用默认多线程行为:

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);
    }
  }

拥有 @TargetApi(11 )意味着如果Lint检测到我正在使用比我的 android:minSdkVersion 更新的东西,但直到API Level 11,Lint不会抱怨。在这种情况下,这是有效的。但是,如果我修改了此方法来引用直到API Level 14之前未添加的内容,那么Lint错误将再次出现,因为我的 @TargetApi(11)注释说我只是修复了API 11级和以上的代码,而不是上面的API Level 14和以下

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 above, not API Level 14 and below above.

使用 @SuppressLint('NewApi'),我将丢失任何 API级别的Lint错误,无论我的代码参考我的代码设置为处理。

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.

因此, @TargetApi 是首选注释,因为它允许你告诉建立工具好的,我修正了这个类别的问题在一个更细致的方式。

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.

这篇关于更好的是:@SuppressLint或@TargetApi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆