Android应用在不同版本间兼容性处理
来源:互联网 发布:阿里云子账号登录 编辑:程序博客网 时间:2024/05/18 02:16
相信开发过一段Android的都被Android中的兼容性问题给折腾过,有时这确实很无奈,Android被不同的厂商改的七的。在Android系统中向下兼容性比较差,但是一个应用APP经过处理还是可以在各个版本间运行的。向下兼容性不好,不同版本的系统其API版本也不同,自然有些接口也不同,新的平台不能使用旧的API,旧的平台也使用不了新的API。
为了应用APP有更好的兼容性,咱们可以利用高版本的SDK开发应用,并在程序运行时(Runtime)对应用所运行的平台判断,旧平台使用旧的API,而新平台可使用新的API,这样可以较好的提高软件兼容性。
Check System Version at Runtime(在软件运行时检查判断系统版本)
Android provides a unique code for each platform version in theBuild
constants class. Use these codes within your app to build conditions that ensure the code thatdepends on higher API levels is executed only when those APIs are available on the system.
private void setUpActionBar() { // Make sure we're running on Honeycomb or higher to use ActionBar APIs if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); }}
Note: When parsing XML resources, Android ignores XML attributes that aren’t supported by the current device. So you can safely use XML attributes thatare only supported by newer versions without worrying about older versions breaking when theyencounter that code. For example, if you set thetargetSdkVersion="11"
, your app includes theActionBar
by defaulton Android 3.0 and higher. To then add menu items to the action bar, you need to setandroid:showAsAction="ifRoom"
in your menu resource XML. It's safe to do this in a cross-version XML file, because the older versions of Android simply ignore theshowAsAction
attribute (that is, youdo not need a separate version inres/menu-v11/
).
从上面可以知道Android为我们提供了一个常量类Build,其中最主要是Build中的两个内部类VERSION和VERSION_CODES,
VERSION表示当前系统版本的信息,其中就包括SDK的版本信息,用于成员SDK_INT表示;
对于VERSION_CODES在SDK开发文档中时这样描述的,Enumeration of the currently known SDK version codes. These are the values that can be found inSDK
. Version numbers increment monotonically with each official platform release.
其成员就是一些从最早版本开始到当前运行的系统的一些版本号常量。
在我们自己开发应用过程中,常常使用如下的代码形式判断运行新API还是旧的API:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // 包含新API的代码块 } else { // 包含旧的API的代码块 }辛苦了这么久 ,发一张萌萌的图片。
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理 .
- Android应用在不同版本间兼容性处理 .
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android应用在不同版本间兼容性处理
- Android进阶之在不同版本间兼容性处理
- Android不同版本间兼容性处理
- Android 不同版本间的兼容性
- Data Pump在不同数据库版本间的兼容性
- Android 高版本API方法在低版本系统上的兼容性处理
- POJ 题目1470 Closest Common Ancestors(LCA)
- 内部类
- Two Sum
- 国企如何发展自己的信息化队伍
- SD-如何在SAP标准IDOC中增加自定义字段传输
- Android应用在不同版本间兼容性处理
- 关于自定义View思考与实践1
- JavaWeb文件上传图片到非工程目录,然后前端访问
- 判断鼠标从什么方向进入元素
- 单链表[建表、输出、插入、删除、逆序]
- 使用NPOI根据DataSet(DataTable)创建Excel
- Linux信号(signal) 机制分析
- s5pv210 的中断向量表及中断在Linux下虚拟地址和物理地址的定义
- java中字符串替换-String.replace StringreplaceAll