Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD

简介:

Build.VERSION.SDK_INT是系统的版本,Build.VERSION_CODES.GINGERBREAD是版本号。

 

到VERSION.SDK_INT不禁诧异,这是何物?!

看API的定义,如下:

 

[java]  view plain copy print ?
 
  1. public static final int SDK_INT  
  2.   
  3. Since: API Level 4  
  4. The user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES.  


原来是一个常量值。但是这个常量值可以根据系统的不同而不同哟!为了揭开其神秘的面纱,将源码ctrl如下:

 

 

[java]  view plain copy print ?
 
  1. /** 
  2.   * The user-visible SDK version of the framework; its possible 
  3.   * values are defined in {@link Build.VERSION_CODES}. 
  4.   */  
  5.   public static final int SDK_INT = SystemProperties.getInt(  
  6.                "ro.build.version.sdk", 0);  


可以看出,获取系统属性,类似Java中获取系统属性值。

 

研究一下 SystemProperties 这个类,知道该类没有在API中出现,Android并没有开放这个API接口。
VERSION.SDK_INT 常量,在开发过程中还是比较有用的,为了做到平台兼容性,可以使用该值做一些判断,防止API调用过时或者消失。

示例:

 

  1. int currentVersion = android.os.Build.VERSION.SDK_INT;  
  2. if(currentVersion == android.os.Build.VERSION_CODES.ECLAIR_MR1) {  
  3.     // 2.1  
  4. else if(currentVersion == android.os.Build.VERSION_CODES.FROYO) {  
  5.     // 2.2  
  6. else if(currentVersion == android.os.Build.VERSION_CODES.GINGERBREAD) {  
  7.     // 2.3  
  8. else if(currentVersion == android.os.Build.VERSION_CODES.HONEYCOMB) {  
  9.     // 3.0  
  10. }  

还如,判断如果设备不是3.0(平板操作系统)的话,就设置不显示标题

  1. if (VERSION.SDK_INT != 11) {  
  2.       getWindow().requestFeature(Window.FEATURE_NO_TITLE);  
  3. }  

这些常量位于android.os.Build.VERSION_CODES这个内部类中:



本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/6024421.html,如需转载请自行联系原作者

相关文章
|
Android开发 开发者
解决No version of NDK matched the requested version问题
一个本来好好的项目,突然在运行的时候报错: No version of NDK matched the requested version 21.0.6113669. Versions available locally: 20.1.5948944 网上很多人建议在build.gradle中添加ndk,如下: android { ... ndkVersion '20.1.5948944' } 复制代码 也确实可以解决问题,但是本来一个对ndk版本没有强依赖的项目,现在固定在一个版本上,其他开发者也必须有这个具体版本才能正常编译运行。
1348 0
|
23天前
|
Linux
【linux】报错pkg_resources.extern.packaging.version.InvalidVersion: Invalid version: ‘0.23ubuntu1’
【linux】报错pkg_resources.extern.packaging.version.InvalidVersion: Invalid version: ‘0.23ubuntu1’
|
Java 应用服务中间件 Android开发
Description Resource Path LocationType Java compiler level does not match the version of the instal
Description Resource Path LocationType Java compiler level does not match the version of the instal
172 0
Description Resource Path LocationType Java compiler level does not match the version of the instal
Plugin [id: ‘com.github.kt3k.coveralls‘, version: ‘2.8.2‘] was not found in any of the following sou
Plugin [id: ‘com.github.kt3k.coveralls‘, version: ‘2.8.2‘] was not found in any of the following sou
85 0
|
开发工具
Failed to find Build Tools revision 25.0.2
Failed to find Build Tools revision 25.0.2
79 0
|
开发工具 Android开发
Failed to find Build Tools revision 28.0.3
Failed to find Build Tools revision 28.0.3
229 0
Failed to find Build Tools revision 28.0.3
|
Android开发
【错误记录】Android Studio 编译报错 ( cannot open this project, please retry with version 4.2 or newer. )
【错误记录】Android Studio 编译报错 ( cannot open this project, please retry with version 4.2 or newer. )
321 0
【错误记录】Android Studio 编译报错 ( cannot open this project, please retry with version 4.2 or newer. )
解决Minimum supported Gradle version is 3.3. Current version is 2.14.1问题
(创建于2017/5/16) 首先要搞懂两个概念,gradle版本和gradle插件版本,比如我现在as升级到了2.3,那么2.3就是gradle版本,2.14.1就是gradle插件版本,这两个概念的一一对应的,而之所以出现这个报错信息,就是因为当前项目的gradle插件版本是2.14.1,而gradle2.3对应的插件版本就是3.3,所以导致了报错。
8288 0