天天看點

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

原文連結:https://www.liaohuqiu.net/cn/posts/using-high-api-level-method-compatibly/

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版本通過其他方法提供功能實作。

繼續閱讀