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:
- Determine the runtime version, this method is not called in the low-version system
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:
- Use to
@TargeApi($API_LEVEL)
make can be compiled through, not recommended @SuppressLint("NewApi")
;
- Run-time judgment API level; This method is called only in an API level system that is high enough to have this method;
- Ensure functional integrity and ensure that low API versions provide functionality through other methods.
Recommended:
Compatibility processing of the Android high-version API method on the low-version system