高低版本方法兼容

新的方法带来许多便利,但无法在低版本系统上运行,如果兼容性处理不恰当,APP在低版本系统上,运行时将会crash。

本文以一个具体的例子说明如何在使用高API level的方法时处理好兼容性问题。

例子:根据给出路径,获取此路径所在分区的总空间大小。

安卓中的文件存储使用参考中提到:

获取文件系统用量情况,在API level 9及其以上的系统,可直接调用File对象的相关方法,以下需自行计算

一般实现

就此需求而言,API level 9及其以上,调用 File.getTotalSpace() 即可, 但是在API level 8 以下系统File对象并不存在此方法。

如以下方法:

  1. /** 
  2. * Returns the total size in bytes of the partition containing this path. * Returns 0 if this path does not exist. 
  3. * @param path
  4. * @return -1 means path is null, 0 means path is not exist. 
  5. */ 
  6. public static long getTotalSpace(File path) { 
  7.     if (path == null) { 
  8.         return -1; 
  9.     } 
  10.     return path.getTotalSpace(); 
  11. }

处理无法编译通过

如果minSdkVersion设置为8,那么build时候会报以下错误:

  1. 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. 同时为了保证功能的完整性,需要提供低版本功能实现

    如下:

    1. /**
    2. * Returns the total size in bytes of the partition containing this path.
    3. * Returns 0 if this path does not exist.
    4. *
    5. * @param path
    6. * @return -1 means path is null, 0 means path is not exist.
    7. */
    8. @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    9. // using @TargeApi instead of @SuppressLint("NewApi")
    10. @SuppressWarnings("deprecation")
    11. public static long getTotalSpace(File path) {
    12. if (path == null) {
    13. return -1;
    14. }
    15. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
    16. return path.getTotalSpace();
    17. }
    18. // implements getTotalSpace() in API lower than GINGERBREAD
    19. else {
    20. if (!path.exists()) {
    21. return 0;
    22. } else {
    23. final StatFs stats = new StatFs(path.getPath());
    24. // Using deprecated method in low API level system,
    25. // add @SuppressWarnings("description") to suppress the warning
    26. return (long) stats.getBlockSize() * (long) stats.getBlockCount();
    27. }
    28. }
    29. }

总结

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

  1. @TargeApi($API_LEVEL) 使可以编译通过, 不建议使用@SuppressLint("NewApi");
  2. 运行时判断API level; 仅在足够高,有此方法的API level系统中,调用此方法;
  3. 保证功能完整性,保证低API版本通过其他方法提供功能实现。
     




posted @ 2016-01-04 12:54  风倾清凌  阅读(695)  评论(0编辑  收藏  举报