以编程方式获取设备的Android API级别?
如何找出我的设备使用的API级别?
固件版本是1.6。 这是否意味着它使用API Level 4?
您可以通过系统常量(Build.VERSION.SDK_INT)以编程方式获取API级别。 例如,您可以按照以下方式运行一些需要更新API的代码(如果当前设备的API级别至less为4,则会执行该代码):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) { }
要获取用户可见的Android版本,请使用:
Build.VERSION.RELEASE
你需要什么: http : //developer.android.com/reference/android/os/Build.VERSION_CODES.html
Code Name Version Api level (no code name) 1.0 API level 1 (no code name) 1.1 API level 2 Cupcake 1.5 API level 3, NDK 1 Donut 1.6 API level 4, NDK 2 Eclair 2.0 API level 5 Eclair 2.0.1 API level 6 Eclair 2.1 API level 7, NDK 3 Froyo 2.2.x API level 8, NDK 4 Gingerbread 2.3 - 2.3.2 API level 9, NDK 5 Gingerbread 2.3.3 - 2.3.7 API level 10 Honeycomb 3.0 API level 11 Honeycomb 3.1 API level 12, NDK 6 Honeycomb 3.2.x API level 13 Ice Cream Sandwich 4.0.1 - 4.0.2 API level 14, NDK 7 Ice Cream Sandwich 4.0.3 - 4.0.4 API level 15, NDK 8 Jelly Bean 4.1.x API level 16 Jelly Bean 4.2.x API level 17 Jelly Bean 4.3.x API level 18 KitKat 4.4 - 4.4.2 API level 19 KitKat (for wearable)4.4 API level 20 Lollipop 5.0 API level 21 Lollipop 5.1 API level 22 Marshmallow 6.0 API level 23 Nougat 7.0 API level 24 Nougat 7.1.1 API level 25 Oreo 8.0 API level 26
对于API级别> = 4 ,可以使用Build.VERSION.SDK_INT
private String getAndroidVersion(int sdk) { switch (sdk) { case 1: return "Android 1.0"; case 2: return "Petit Four (Android 1.1)"; case 3: return "Cupcake (Android 1.5)"; case 4: return "Donut (Android 1.6)"; case 5: return "Eclair (Android 2.0)"; case 6: return "Eclair (Android 2.0.1)"; case 7: return "Eclair (Android 2.1)"; case 8: return "Froyo (Android 2.2)"; case 9: return "Gingerbread (Android 2.3)"; case 10: return "Gingerbread (Android 2.3.3)"; case 11: return "Honeycomb (Android 3.0)"; case 12: return "Honeycomb (Android 3.1)"; case 13: return "Honeycomb (Android 3.2)"; case 14: return "Ice Cream Sandwich (Android 4.0)"; case 15: return "Ice Cream Sandwich (Android 4.0.3)"; case 16: return "Jelly Bean (Android 4.1)"; case 17: return "Jelly Bean (Android 4.2)"; case 18: return "Jelly Bean (Android 4.3)"; case 19: return "KitKat (Android 4.4)"; case 20: return "KitKat Watch (Android 4.4)"; case 21: return "Lollipop (Android 5.0)"; case 22: return "Lollipop (Android 5.1)"; case 23: return "Marshmallow (Android 6.0)"; case 24: return "Nougat (Android 7.0)"; case 25: return "Nougat (Android 7.1.1)"; case 26: return "Oreo (Android 8.0)"; default: return ""; } }
用法
String android_os = getAndroidVersion(android.os.Build.VERSION.SDK_INT);
好简单:
String manufacturer = Build.MANUFACTURER; String model = Build.MODEL; int version = Build.VERSION.SDK_INT; String versionRelease = Build.VERSION.RELEASE; Log.e("MyActivity", "manufacturer " + manufacturer + " \n model " + model + " \n version " + version + " \n versionRelease " + versionRelease );
输出:
E/MyActivity: manufacturer samsung model SM-T310 version 19 versionRelease 4.4.2
为了让你的设备最新的API:
int MyDeviceAPI = Build.VERSION.SDK_INT;
在第一个版本中使用了Build.VERSION.SDK
String myAPI= Build.VERSION.SDK;
但现在已经弃用!
SDK此字段在API级别4中已被弃用。使用SDK_INT可轻松将其作为整数获取。
现在您必须使用: SDK_INT在API级别4中添加:
int myAPI = Build.VERSION.SDK_INT;
SDK_INT框架的用户可见的SDK版本; 其可能的值在Build.VERSION_CODES中定义。
更多信息:
http://developer.android.com/reference/android/os/Build.VERSION.html
您可以获取设备的信息,如下所示。
Log.d(TAG, "RELEASE : " + Build.VERSION.RELEASE); Log.d(TAG, "SDK : " + Build.VERSION.SDK_INT);
并可以从OS获取Android版本代码历史logging。
for (Field field : Build.VERSION_CODES.class.getDeclaredFields()) { field.setAccessible(true); Log.d(TAG, field.get(this) + " -> " + field.getName()); }
这是输出。
RELEASE : 5.1.1 SDK : 22 1 -> BASE 2 -> BASE_1_1 3 -> CUPCAKE 10000 -> CUR_DEVELOPMENT 4 -> DONUT 5 -> ECLAIR 6 -> ECLAIR_0_1 7 -> ECLAIR_MR1 8 -> FROYO 9 -> GINGERBREAD 10 -> GINGERBREAD_MR1 11 -> HONEYCOMB 12 -> HONEYCOMB_MR1 13 -> HONEYCOMB_MR2 14 -> ICE_CREAM_SANDWICH 15 -> ICE_CREAM_SANDWICH_MR1 16 -> JELLY_BEAN 17 -> JELLY_BEAN_MR1 18 -> JELLY_BEAN_MR2 19 -> KITKAT 20 -> KITKAT_WATCH 21 -> L 21 -> LOLLIPOP 22 -> LOLLIPOP_MR1
所以,结合这些和官方的版本列表 。
API VERSION_CODE VERSION CODE_NAME 1 BASE 1 (no code name) 2 BASE_1_1 1.1 (no code name) 3 CUPCAKE 1.5 Cupcake 4 DONUT 1.6 Donut 5 ECLAIR 2 Eclair 6 ECLAIR_0_1 2.0.1 Eclair 7 ECLAIR_MR1 2.1 Eclair 8 FROYO 2.2.x Froyo 9 GINGERBREAD 2.3 - 2.3.2 Gingerbread 10 GINGERBREAD_MR1 2.3.3 - 2.3.7 Gingerbread 11 HONEYCOMB 3 Honeycomb 12 HONEYCOMB_MR1 3.1 Honeycomb 13 HONEYCOMB_MR2 3.2.x Honeycomb 14 ICE_CREAM_SANDWICH 4.0.1 - 4.0.2 Ice Cream Sandwich 15 ICE_CREAM_SANDWICH_MR1 4.0.3 - 4.0.4 Ice Cream Sandwich 16 JELLY_BEAN 4.1.x Jelly Bean 17 JELLY_BEAN_MR1 4.2.x Jelly Bean 18 JELLY_BEAN_MR2 4.3.x Jelly Bean 19 KITKAT 4.4 - 4.4.4 KitKat 20 KITKAT_WATCH 4.4W KitKat Watch 21 LOLLIPOP 5 Lollipop 22 LOLLIPOP_MR1 5.1 Lollipop 23 M 6 Marshmallow 24 N 7 Nougat 25 N_MR1 7.1 Nougat
这就是说,API25的version_code是N_MR1。