Differences between @SuppressLint ("Newapi") and @targetapi ()

Source: Internet
Author: User

Transferred from: http://blog.csdn.NET/wbshuang09/article/details/44920549
In Android code, we sometimes use a higher method than the Android:minsdkversion version we set in Androidmanifest, when the compiler prompts for a warning, and the workaround is to add @suppresslint to the method (" Newapi ") or @targetapi ().

What's the difference between them, very simple,

@SuppressLint ("Newapi") block all new APIs in order to use the method reported by Android Lint error

@TargetApi () block only a new API to use the lint error of a method reported Android

For example, a method that uses the new Api9 method, and the android:minsdkversion=8 of the project settings, add @suppresslint ("Newapi") and @targetapi on the method (Build.version_ CODES. Gingerbread) All can, the above is the general situation.

And when you refer to a method in this method that api11 only joins, @TargetApi (Build.version_codes. Gingerbread) Annotated method and error, and @suppresslint ("Newapi") will not error, this is the difference.

Of course, regardless of which annotation you use, the role is only to block Android lint errors, so in the method also to determine the version to do different operations, such as:

[Java]View Plaincopy
    1. if (Build.VERSION.SDK_INT >= build.version_codes. Gingerbread) {
    2. //  
    3. } else {//Pre gingerbread
    4. //  
    5. }
@SuppressLint callout ignores the specified warning @suppresslint callout ignores the specified warning @suppresslint label ignores the specified warning this is the lint ToolTip for the Android tape, lint the official argument is improving Your code with lint, it should be to help improve the codes, if you do not want to use, you can right-click the project, and then in Android tools, select clear Lint marker There is no such error

Differences between @SuppressLint ("Newapi") and @targetapi ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.