Android High-Version API approach to compatibility handling on lower-version systems _android

Source: Internet
Author: User

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

The new approach brings many conveniences, but it cannot 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 provides a concrete example of how to handle compatibility issues when using a method of high API level.

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

Mentioned in the Andong file storage usage Reference:

To obtain the file system usage, in the API level 9 and above the system, can directly call the file object related methods, the following need to calculate

General implementation

For this requirement, API level 9 and above, call File.gettotalspace (), but this method does not exist in the system File object below API Level 8.

such as the following methods:

/**
 * Returns The total size in bytes of the partition containing this path.
 * Returns 0 If this path is does not exist.
 * 
 * @param path
 * @return-1 means path is NULL, 0 means path are not exist.
 */public
static long Gettotalspace (File path) {
  if (path = = null) {
    return-1
  }
  return Path.gettotalspace ();
}

Processing cannot be compiled through

If the minsdkversion is set to 8, the following error will be reported at build time:

Call requires API Level 9 (current min. is 8)

To compile it, you can add @SuppressLint ("Newapi") or @TargeApi (9).

Using @targeapi ($API _level) to explicitly indicate the API level requirements of the method, rather than the @suppresslint ("Newapi");

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

The right approach

To run without an error, you need to:

Determines the runtime version and does not call this method in the lower version system
At the same time, in order to ensure the integrity of the function, we need to provide low version function

As follows:

/** * Returns The total size in bytes to the partition containing this path.
 * Returns 0 If this path is does not exist.
 * * @param path * @return-1 means path is NULL, 0 means path are not exist. * * @TargetApi (build.version_codes. Gingerbread)//using @TargeApi instead of @SuppressLint ("Newapi") @SuppressWarnings ("deprecation") public static long
  Gettotalspace (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 At low API level system,//Add @SuppressWarnings ("description") to suppress the warning
    Return (long) stats.getblocksize () * (long) stats.getblockcount (); }
  }
}

Summarize

Methods that use higher than the Minsdkversion API level require:

    1. Using @targeapi ($API _level) enables compilation to pass, does not recommend the use of @suppresslint ("Newapi");
    2. Run time to judge API level; Call this method only if it is high enough to have an API level system with this method;
    3. Ensure functional integrity to ensure that low API versions provide functionality through other methods.

This article hopes to help you solve the Android version API compatibility issue! Thank you for your support to this site!

Related Article

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.