以编程方式获取设备的Android API级别?

时间:2010-05-04 20:37:28

标签: android

如何找出我的设备使用的API级别?

固件版本为1.6。这是否意味着它使用API​​级别4?

7 个答案:

答案 0 :(得分:91)

您可以通过系统常量(Build.VERSION.SDK_INT)以编程方式获取API级别。例如,您可以通过以下方式运行一些需要更新API的代码(如果当前设备的API级别至少为4,它将执行)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {

}

要获取用户可见的Android版本,请使用:

Build.VERSION.RELEASE

答案 1 :(得分:30)

您需要什么:http://developer.android.com/reference/android/os/Build.VERSION_CODES.html

SDK_INT value        Build.VERSION_CODES        Human Version Name       
    1                  BASE                      Android 1.0 (no codename)
    2                  BASE_1_1                  Android 1.1 Petit Four
    3                  CUPCAKE                   Android 1.5 Cupcake
    4                  DONUT                     Android 1.6 Donut
    5                  ECLAIR                    Android 2.0 Eclair
    6                  ECLAIR_0_1                Android 2.0.1 Eclair                  
    7                  ECLAIR_MR1                Android 2.1 Eclair
    8                  FROYO                     Android 2.2 Froyo
    9                  GINGERBREAD               Android 2.3 Gingerbread
   10                  GINGERBREAD_MR1           Android 2.3.3 Gingerbread
   11                  HONEYCOMB                 Android 3.0 Honeycomb
   12                  HONEYCOMB_MR1             Android 3.1 Honeycomb
   13                  HONEYCOMB_MR2             Android 3.2 Honeycomb
   14                  ICE_CREAM_SANDWICH        Android 4.0 Ice Cream Sandwich
   15                  ICE_CREAM_SANDWICH_MR1    Android 4.0.3 Ice Cream Sandwich
   16                  JELLY_BEAN                Android 4.1 Jellybean
   17                  JELLY_BEAN_MR1            Android 4.2 Jellybean
   18                  JELLY_BEAN_MR2            Android 4.3 Jellybean
   19                  KITKAT                    Android 4.4 KitKat
   20                  KITKAT_WATCH              Android 4.4 KitKat Watch
   21                  LOLLIPOP                  Android 5.0 Lollipop
   22                  LOLLIPOP_MR1              Android 5.1 Lollipop
   23                  M                         Android 6.0 Marshmallow
   24                  N                         Android 7.0 Nougat
   25                  N_MR1                     Android 7.1.1 Nougat
   26                  O                         Android 8.0 Oreo
   27                  O_MR1                     Android 8 Oreo MR1
   28                  P                         Android P
  10000                CUR_DEVELOPMENT           Current Development Version

答案 2 :(得分:8)

对于 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)";
        case 27: return  "Oreo"                + "(Android 8.1)";
        case 28: return  "Pie"                 + "(Android 9.0)";
        default: return  "";
    }
}

<强>用法

String android_os = getAndroidVersion(android.os.Build.VERSION.SDK_INT);

答案 3 :(得分:4)

非常简单:

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

答案 4 :(得分:3)

要获取设备当前的API:

int MyDeviceAPI = Build.VERSION.SDK_INT;

答案 5 :(得分:3)

第一个版本中的

使用了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

答案 6 :(得分:2)

您可以获取设备的信息,如下所示。

Log.d(TAG, "RELEASE : " + Build.VERSION.RELEASE);
Log.d(TAG, "SDK     : " + Build.VERSION.SDK_INT);

并且可以从操作系统获取Android版本代码历史记录。

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

所以,将这些和official version list结合起来。

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

This说,API25的version_code是N_MR1。