FAQ_1_陌生的VERSION.SDK_INT

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

看API的定义,如下:

public static final int SDK_INT    
Since: API Level 4  
The user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES. 


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

/** 
  * The user-visible SDK version of the framework; its possible 
  * values are defined in {@link Build.VERSION_CODES}. 
  */  
  public static final int SDK_INT = SystemProperties.getInt(  
               "ro.build.version.sdk", 0);  

  


 

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

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

示例:

int currentVersion = android.os.Build.VERSION.SDK_INT;  
if(currentVersion == android.os.Build.VERSION_CODES.ECLAIR_MR1) {  
    // 2.1  
} else if(currentVersion == android.os.Build.VERSION_CODES.FROYO) {  
    // 2.2  
} else if(currentVersion == android.os.Build.VERSION_CODES.GINGERBREAD) {  
    // 2.3  
} else if(currentVersion == android.os.Build.VERSION_CODES.HONEYCOMB) {  
    // 3.0  
}  
 

  

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

   

   if (VERSION.SDK_INT != 11) {  

        getWindow().requestFeature(Window.FEATURE_NO_TITLE);  

   } 

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

原文地址:https://www.cnblogs.com/McCa/p/4467691.html