Android 高版本API方法在低版本系统上的兼容性处理
Android版本更替,新的版本带来新的特性,新的方法。
新的方法带来许多便利,但无法在低版本系统上运行,如果兼容性处理不恰当,APP在低版本系统上,运行时将会crash。
本文以一个具体的例子说明如何在使用高APIlevel的方法时处理好兼容性问题。
例子:根据给出路径,获取此路径所在分区的总空间大小。
在安卓中的文件存储使用参考中提到:
获取文件系统用量情况,在APIlevel9及其以上的系统,可直接调用File对象的相关方法,以下需自行计算
一般实现
就此需求而言,APIlevel9及其以上,调用File.getTotalSpace()即可,但是在APIlevel8以下系统File对象并不存在此方法。
如以下方法:
/** *Returnsthetotalsizeinbytesofthepartitioncontainingthispath. *Returns0ifthispathdoesnotexist. * *@parampath *@return-1meanspathisnull,0meanspathisnotexist. */ publicstaticlonggetTotalSpace(Filepath){ if(path==null){ return-1; } returnpath.getTotalSpace(); }
处理无法编译通过
如果minSdkVersion设置为8,那么build时候会报以下错误:
CallrequiresAPIlevel9(currentminis8)
为了编译可以通过,可以添加@SuppressLint("NewApi")或者@TargeApi(9)。
用@TargeApi($API_LEVEL)显式表明方法的APIlevel要求,而不是@SuppressLint("NewApi");
但是这样只是能编译通过,到了APIlevel8的系统运行,将会引发java.lang.NoSuchMethodError。
正确的做法
为了运行时不报错,需要:
判断运行时版本,在低版本系统不调用此方法
同时为了保证功能的完整性,需要提供低版本功能实现
如下:
/** *Returnsthetotalsizeinbytesofthepartitioncontainingthispath. *Returns0ifthispathdoesnotexist. * *@parampath *@return-1meanspathisnull,0meanspathisnotexist. */ @TargetApi(Build.VERSION_CODES.GINGERBREAD) //using@TargeApiinsteadof@SuppressLint("NewApi") @SuppressWarnings("deprecation") publicstaticlonggetTotalSpace(Filepath){ if(path==null){ return-1; } if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD){ returnpath.getTotalSpace(); } //implementsgetTotalSpace()inAPIlowerthanGINGERBREAD else{ if(!path.exists()){ return0; }else{ finalStatFsstats=newStatFs(path.getPath()); //UsingdeprecatedmethodinlowAPIlevelsystem, //add@SuppressWarnings("description")tosuppressthewarning return(long)stats.getBlockSize()*(long)stats.getBlockCount(); } } }
总结
在使用高于minSdkVersionAPIlevel的方法需要:
- 用@TargeApi($API_LEVEL)使可以编译通过,不建议使用@SuppressLint("NewApi");
- 运行时判断APIlevel;仅在足够高,有此方法的APIlevel系统中,调用此方法;
- 保证功能完整性,保证低API版本通过其他方法提供功能实现。
通过此文希望能帮助到您,解决Android版本API兼容性问题!谢谢大家对本站的支持!