1. 程式人生 > >Android 高版本API方法在低版本系統上的兼容性處理

Android 高版本API方法在低版本系統上的兼容性處理

file mea param data- count clas eve lint -i

Android 版本更替,新的版本帶來新的特性,新的方法。

新的方法帶來許多便利,但無法在低版本系統上運行,如果兼容性處理不恰當,APP在低版本系統上,運行時將會crash。

本文以一個具體的例子說明如何在使用高API level的方法時處理好兼容性問題。

例子:根據給出路徑,獲取此路徑所在分區的總空間大小。

在安卓中的文件存儲使用參考中提到:

獲取文件系統用量情況,在API level 9及其以上的系統,可直接調用File對象的相關方法,以下需自行計算

一般實現

就此需求而言,API level 9及其以上,調用 File.getTotalSpace() 即可, 但是在API level 8 以下系統File

對象並不存在此方法。

如以下方法:

/**
 * 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();
}
處理無法編譯通過

如果minSdkVersion設置為8,那麽build時候會報以下錯誤:

Call requires API level 9 (current min is 8)

為了編譯可以通過,可以添加 @SuppressLint("NewApi") 或者 @TargeApi(9)

@TargeApi($API_LEVEL)顯式表明方法的API level要求,而不是@SuppressLint("NewApi");

但是這樣只是能編譯通過,到了API level8的系統運行,將會引發 java.lang.NoSuchMethodError

正確的做法

為了運行時不報錯, 需要:

  1. 判斷運行時版本,在低版本系統不調用此方法
  2. 同時為了保證功能的完整性,需要提供低版本功能實現

    如下:

    /**
     * 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.
     */
    @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 in low API level system, 
                // add @SuppressWarnings("description") to suppress the warning
                return (long) stats.getBlockSize() * (long) stats.getBlockCount();
            }
        }
    }
    

總結

在使用高於minSdkVersion API level的方法需要:

  1. @TargeApi($API_LEVEL) 使可以編譯通過, 不建議使用@SuppressLint("NewApi");
  2. 運行時判斷API level; 僅在足夠高,有此方法的API level系統中,調用此方法;
  3. 保證功能完整性,保證低API版本通過其他方法提供功能實現。
推薦:

Android 高版本API方法在低版本系統上的兼容性處理