新的方法帶來許多便利,但沒法在低版本系統上運行,若是兼容性處理不恰當,APP在低版本系統上,運行時將會crash。java
本文以一個具體的例子說明如何在使用高API level的方法時處理好兼容性問題。android
例子:根據給出路徑,獲取此路徑所在分區的總空間大小。ui
在安卓中的文件存儲使用參考中提到:this
獲取文件系統用量狀況,在API level 9及其以上的系統,可直接調用File對象的相關方法,如下需自行計算spa
就此需求而言,API level 9及其以上,調用 File.getTotalSpace() 便可, 可是在API level 8 如下系統File對象並不存在此方法。.net
如如下方法:code
- {
- if (path == null) {
- return -1;
- }
- return path.getTotalSpace();
- }
/** * 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。
爲了運行時不報錯, 須要:
同時爲了保證功能的完整性,須要提供低版本功能實現
以下:
/** * 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的方法須要: