android多个版本号区别,Android不同版本间兼容性处理

在Android系统中向下兼容性比较差,但是一个应用APP经过处理还是可以在各个版本间运行的。向下兼容性不好,不同版本的系统其API版本也不同,自然有些接口也不同,主要是旧的平台使用不了新的API。

但这并不代表每个平台都需要一个单独的apk,也不代表使用了新sdk的apk在低版本系统手机上不能运行。可以在高SDK上开发,并在程序中作版本判断,低版本运行环境使用旧的API

那么,如何在软件运行时做出这样的判断呢?

在Android SDK开发文档中有段话这样的话:

在运行时检查系统版本

Android provides a unique code for each platform version in theprivatevoidsetUpActionBar(){// Make sure we're running on Honeycomb or higher to use ActionBar APIsif(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){ActionBaractionBar=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 the android: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, you do 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的代码块

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值