什么是更好:@燮pressLint或@TargetApi? [英] What is better: @SuppressLint or @TargetApi?

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

问题描述

我有一个关于我的应用程序的问题 StrictMode 并添加了code段基本上禁用 StrictModeHelper 。然而,林特抱怨 setThreadPolicy()现在建议以添加

  @燮pressLint'NewApi
 

  @TargetApi(Build.VERSION_ codeS.GINGERBREAD)
 

到视图的的onCreate()事件。

哪种方法prefered ..或者是他们基本上做同样的?

解决方案
  

我在我的关于StrictMode应用程序的问题,并且增加了code段,基本上禁止StrictModeHelper

天才程序员会解决他们的网络错误。

  

哪种方法prefered ..或者是他们基本上做同样的?

@TargetApi @燮pressLint 拥有相同的核心作用:他们想喝preSS皮棉错误。

不同的是,与 @TargetApi ,声明,通过参数,API是什么水平,你在你的code已经解决,从而使错误可以弹出再次,如果你以后修改的方法来尝试引用的东西比引 @TargetApi API级别更新。

例如,假设而不是阻塞的 StrictMode 抱怨你的网络臭虫,你试图解决的 AsyncTask的<发行/ code>正在连载的较新版本的Andr​​oid系统。您的code这样的方法,以选择到较新设备上的线程池,并使用旧设备的默认多线程的行为:

  @TargetApi(11)
  静态公网&LT; T&GT;无效executeAsyncTask(AsyncTask的&LT;?T,,&GT;的任务,
                                          的... PARAMS){
    如果(Build.VERSION.SDK_INT&GT; = Build.VERSION_ codeS.HONEYCOMB){
      task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,则params);
    }
    其他 {
      task.execute(PARAMS);
    }
  }
 

@TargetApi(11)意味着如果lint检测,我用比我的 Android的一些新:的minSdkVersion ,但到API级别11,林特不会抱怨。在这种情况下,工作原理。但是,如果我修改了这个方法来引用不加入到API级别14的东西,那么将再次出现掉毛的错误,因为我的 @TargetApi(11)注释说,我只固定的code工作在API级别11及以下,没有API等级14及以下。

使用 @燮pressLint('NewApi'),我就失去了皮棉错误的任意的API级别,不管是什么我的code参考什么我的code设置来处理。

因此​​, @TargetApi 是preferred注解,因为它允许你告诉生成工具以更好了,我解决了这个类别的问题细粒度的方式。

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'

or

@TargetApi(Build.VERSION_CODES.GINGERBREAD)

to the onCreate() event of the view.

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

解决方案

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

Talented programmers would fix their networking bug.

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

@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.

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

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