高低版本方法兼容

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

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

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

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

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

通常實現

就此需求而言,API level 9及其以上,調用 File.getTotalSpace() 便可, 可是在API level 8 如下系統File對象並不存在此方法。.net

如如下方法:code

  
  
  
  
    
    
    
    
  1. { 
  2.     if (path == null) { 
  3.         return -1; 
  4.     } 
  5.     return path.getTotalSpace(); 
  6. }

/** * 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) 
處理沒法編譯經過

若是minSdkVersion設置爲8,那麼build時候會報如下錯誤:對象

  
  
  
  
    
    
    
    

Call requires API level 9 (current min is 8)

爲了編譯能夠經過,能夠添加 @SuppressLint("NewApi") 或者 @TargeApi(9)ip

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

可是這樣只是能編譯經過,到了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版本經過其餘方法提供功能實現。
     


相關文章
相關標籤/搜索