看到VERSION.SDK_INT不由詫異,這是何物?!java
看API的定義,以下:android
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以下:web
/** * 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中獲取系統屬性值。 ui
研究一下 SystemProperties 這個類,知道該類沒有在API中出現,android並無開放這個API接口。
VERSION.SDK_INT 常量,在開發過程當中仍是比較有用的,爲了作到平臺兼容性,能夠使用該值作一些判斷,防止API調用過期或者消失。spa
示例:操作系統
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(平板操做系統)的話,就設置不顯示標題:code
if (VERSION.SDK_INT != 11) { getWindow().requestFeature(Window.FEATURE_NO_TITLE); }
這些常量位於android.os.Build.VERSION_CODES這個內部類中: orm