以编程方式检索 Android API 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3423754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:07:58  来源:igfitidea点击:

Retrieving Android API version programmatically

androidandroid-api-levels

提问by Parth Mehta

Is there any way to get the API version that the phone is currently running?

有没有办法获取手机当前运行的API版本?

回答by ol_v_er

As described in the Android documentation, the SDK level (integer) the phone is running is available in:

如 Android 文档中所述,手机运行的 SDK 级别(整数)可在以下位置获得:

android.os.Build.VERSION.SDK_INT

android.os.Build.VERSION.SDK_INT

The class corresponding to this int is in the android.os.Build.VERSION_CODESclass.

这个int对应的android.os.Build.VERSION_CODES类在类中。

Code example:

代码示例:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
    // Do something for lollipop and above versions
} else{
    // do something for phones running an SDK before lollipop
}

Edit: This SDK_INT is available since Donut (android 1.6 / API4) so make sure your application is not retro-compatible with Cupcake (android 1.5 / API3) when you use it or your application will crash (thanks to Programmer Bruce for the precision).

编辑:此 SDK_INT 自 Donut (android 1.6 / API4) 起可用,因此请确保您的应用程序在使用时与 Cupcake (android 1.5 / API3) 不兼容,否则您的应用程序将崩溃(感谢程序员 Bruce 提供的精确度) .

Corresponding android documentation is hereand here

相应的 android 文档在这里这里

回答by CommonSenseCode

Very easy:

好简单:

   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
    );

Output:

输出:

E/MyActivity:   manufacturer ManufacturerX
                model SM-T310 
                version 19 
                versionRelease 4.4.2

回答by Falcon165o

Build.VERSION.RELEASE;

That will give you the actual numbers of your version; aka 2.3.3 or 2.2. The problem with using Build.VERSION.SDK_INT is if you have a rooted phone or custom rom, you could have a non standard OS (aka my android is running 2.3.5) and that will return a null when using Build.VERSION.SDK_INT so Build.VERSION.RELEASE will work no matter using standard Android version or not !

这将为您提供版本的实际数字;又名 2.3.3 或 2.2。使用 Build.VERSION.SDK_INT 的问题是,如果你有一个根电话或自定义 rom,你可能有一个非标准的操作系统(也就是我的 android 运行 2.3.5)并且在使用 Build.VERSION.SDK_INT 时会返回一个 null所以无论是否使用标准的 Android 版本,Build.VERSION.RELEASE 都可以工作!

To use it, you could just do this;

要使用它,你可以这样做;

String androidOS = Build.VERSION.RELEASE;

回答by nikib3ro

Taking into account all said, here is the code I use for detecting if device has Froyo or newer Android OS (2.2+):

考虑到所有这些,这里是我用来检测设备是否有 Froyo 或更新的 Android OS (2.2+) 的代码:

public static boolean froyoOrNewer() {
    // SDK_INT is introduced in 1.6 (API Level 4) so code referencing that would fail
    // Also we can't use SDK_INT since some modified ROMs play around with this value, RELEASE is most versatile variable
    if (android.os.Build.VERSION.RELEASE.startsWith("1.") ||
        android.os.Build.VERSION.RELEASE.startsWith("2.0") ||
        android.os.Build.VERSION.RELEASE.startsWith("2.1"))
        return false;

    return true;
}

Obviously, you can modify that if condition to take into account 1.0 & 1.5 versions of Android in case you need generic checker. You will probably end up with something like this:

显然,如果您需要通用检查器,您可以修改 if 条件以考虑 1.0 和 1.5 版本的 Android。你可能会得到这样的结果:

// returns true if current Android OS on device is >= verCode 
public static boolean androidMinimum(int verCode) {
    if (android.os.Build.VERSION.RELEASE.startsWith("1.0"))
        return verCode == 1;
    else if (android.os.Build.VERSION.RELEASE.startsWith("1.1")) {
        return verCode <= 2;
    } else if (android.os.Build.VERSION.RELEASE.startsWith("1.5")) {
        return verCode <= 3;
    } else {
        return android.os.Build.VERSION.SDK_INT >= verCode;
    }
}

Let me know if code is not working for you.

如果代码对您不起作用,请告诉我。

回答by Khalid Taha

try this:

尝试这个:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
     // only for gingerbread and newer versions
 }

回答by jvdneste

android.os.Build.VERSION.SDK should give you the value of the API Level. You can easily find the mapping from api level to android version in the android documentation. I believe, 8 is for 2.2, 7 for 2.1, and so on.

android.os.Build.VERSION.SDK 应该为您提供 API 级别的值。您可以在 android 文档中轻松找到从 api 级别到 android 版本的映射。我相信,8 代表 2.2,7 代表 2.1,依此类推。

回答by Parth Mehta

Got it. Its using the getApplicationInfo()method of the Contextclass.

知道了。它使用类的getApplicationInfo()方法Context

回答by PYK

SDK.INT is supported for Android 1.6and up

Android 1.6及更高版本支持 SDK.INT

SDK is supported for all versions

所有版本均支持 SDK

So I do:

所以我这样做:

String sdk_version_number = android.os.Build.VERSION.SDK;

Credits to: CommonsWareover this answer

归功于CommonsWare在这个答案上

回答by Keshav Gera

Its Working Happy Coding

它的工作快乐编码

String versionRelease = BuildConfig.VERSION_NAME;

versionRelease :- 2.1.17

NotePlease make sure your import package is correct ( import package your application package name otherwise it's not work properly)

注意请确保你的导入包是正确的(导入包是你的应用包名,否则无法正常运行)

回答by alex

i prefer have the version as number to be handeled more easyway than i wrote this:

我更喜欢将版本作为编号处理,比我写的更容易:

  public static float getAPIVerison() {

    Float f = null;
    try {
        StringBuilder strBuild = new StringBuilder();
        strBuild.append(android.os.Build.VERSION.RELEASE.substring(0, 2));
        f = new Float(strBuild.toString());
    } catch (NumberFormatException e) {
        Log.e("", "error retriving api version" + e.getMessage());
    }

    return f.floatValue();
}