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:
- Using @targeapi ($API _level) enables compilation to pass, does not recommend the use of @suppresslint ("Newapi");
- Run time to judge API level; Call this method only if it is high enough to have an API level system with this method;
- 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!