Compatibility processing of the Android high-version API method on the low-version system

Source: Internet
Author: User

Android version replacement, new version brings new features, new methods.

The new approach brings a lot of convenience, but it can't run on a lower version of the system, and if the compatibility is not handled properly, the app will be crash on the lower version of the system.

This article shows a specific example of how to handle compatibility issues when using the High API level approach.

Example: Gets the total space size of the partition on which this path resides, based on the given path.

Referenced in the wirelessly file store usage reference:

To obtain the file system usage, in the API level 9 and above the system, you can directly invoke File the relevant methods of the object, the following need to calculate their own

General implementation

For this requirement, API level 9 and above File.getTotalSpace() can be called, but File this method does not exist for system objects below API Level 8.

As in the following ways:

/** * Returns the total size in bytes of the partition containing this path. * Returns 0 if this path does not exist. *  * @param path * @return -1 means path is null, 0 means path is not exist. */public static long getTotalSpace(File path) {    if (path == null) {        return -1;    }    return path.getTotalSpace();}
Processing cannot be compiled by

If minSdkVersion set to 8, then the build will report the following error:

Call requires API level 9 (current min is 8)

In order to compile can pass, can add @SuppressLint("NewApi") or @TargeApi(9) .

@TargeApi($API_LEVEL)the API level required to explicitly indicate the method, but not @SuppressLint("NewApi") ;

But this only can be compiled through, to the API Level8 system run, will be thrown java.lang.NoSuchMethodError .

The right approach

In order to run without error, you need:

  1. Determine the runtime version, this method is not called in the low-version system
  2. In order to ensure the integrity of the function, we need to provide the low-version function

    As follows:

    /** * Returns The total size in bytes of the partition containing this path. * Returns 0 If this path does not exist. * * @param path * @return-1 means path is null and 0 means path is not exist. */@TargetApi (build.version_codes. Gingerbread)//using @TargeApi instead of @SuppressLint ("Newapi") @SuppressWarnings ("deprecation") public static long G    Ettotalspace (File path) {if (path = = null) {return-1; } if (Build.VERSION.SDK_INT >= build.version_codes.    Gingerbread) {return path.gettotalspace (); }//Implements Gettotalspace () in API lower than gingerbread else {if (!path.exists ()) {return        0;            } else {final StatFs stats = new StatFs (Path.getpath ()); Using deprecated method In low API level system,//Add @SuppressWarnings ("description") to suppress the WA        Rning return (Long) stats.getblocksize () * (long) stats.getblockcount (); }    }}
Summarize

Using a method higher than minSdkVersion the API level requires:

    1. Use to @TargeApi($API_LEVEL) make can be compiled through, not recommended @SuppressLint("NewApi") ;
    2. Run-time judgment API level; This method is called only in an API level system that is high enough to have this method;
    3. Ensure functional integrity and ensure that low API versions provide functionality through other methods.
Recommended:Memory leaks should be used leakcanary

Compatibility processing of the Android high-version API method on the low-version system

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.