如何查看Android系统版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3093365/
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 08:39:51  来源:igfitidea点击:

How can I check the system version of Android?

androidversionsystem

提问by davs

Does anyone know how can I check the system version (e.g. 1.0, 2.2, etc.) programatically?

有谁知道我怎么能检查系统版本(例如1.02.2等)编程?

采纳答案by Robby Pond

Check android.os.Build.VERSION.

检查android.os.Build.VERSION

  • CODENAME: The current development codename, or the string "REL" if this is a release build.
  • INCREMENTAL: The internal value used by the underlying source control to represent this build.
  • RELEASE: The user-visible version string.
  • CODENAME:当前的开发代号,如果这是发布版本,则为字符串“REL”。
  • INCREMENTAL:底层源代码管理用于表示此构建的内部值。
  • RELEASE:用户可见的版本字符串。

回答by ATom

Example how to use it:

示例如何使用它:

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

回答by jpotts18

Build.Versionis the place go to for this data. Here is a code snippet for how to format it.

Build.Version是存放这些数据的地方。这是一个关于如何格式化它的代码片段。

public String getAndroidVersion() {
    String release = Build.VERSION.RELEASE;
    int sdkVersion = Build.VERSION.SDK_INT;
    return "Android SDK: " + sdkVersion + " (" + release +")";
}

Looks like this "Android SDK: 19 (4.4.4)"

看起来像这样“Android SDK:19(4.4.4)”

回答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 none 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 what!

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

回答by paulcab

For checking device version which is greater than or equal to Marshmallow ,use this code.

要检查大于或等于 Marshmallow 的设备版本,请使用此代码。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){

    }

for ckecking others just change the VERSION_CODES like,
K for kitkat,
L for loolipop N for Nougat and so on...

为了检查其他人,只需更改 VERSION_CODES,例如
K 代表奇巧,
L 代表 loolipop N 代表牛轧糖等等......

回答by Dave Webb

You can find out the Android version looking at Build.VERSION.

您可以查看 Android 版本Build.VERSION

The documentation recommends you check Build.VERSION.SDK_INTagainst the values in Build.VERSION_CODES.

该文档建议您检查Build.VERSION.SDK_INT对中的值Build.VERSION_CODES

This is fine as long as you realise that Build.VERSION.SDK_INTwas only introduced in API Level 4, which is to say Android 1.6 (Donut). So this won't affect you, but if you did want your app to run on Android 1.5 or earlier then you would have to use the deprecated Build.VERSION.SDKinstead.

只要您意识到它Build.VERSION.SDK_INT仅在 API 级别 4 中引入,即 Android 1.6 (Donut),就可以了。所以这不会影响您,但如果您确实希望您的应用程序在 Android 1.5 或更早版本上运行,那么您将不得不使用已弃用的版本Build.VERSION.SDK

回答by Micha? Kisiel

I can't comment on the answers, but there is a huge mistake in Kaushik's answer: SDK_INT is not the same as system version but actually refers to API Level.

我无法对答案发表评论,但 Kaushik 的答案中有一个巨大的错误:SDK_INT 与系统版本不同,但实际上指的是 API 级别。

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
    //this code will be executed on devices running ICS or later
}

The value Build.VERSION_CODES.ICE_CREAM_SANDWICHequals 14. 14 is the API level of Ice Cream Sandwich, while the system version is 4.0. So if you write 4.0, your code will be executed on all devices starting from Donut, because 4 is the API level of Donut (Build.VERSION_CODES.DONUTequals 4).

该值Build.VERSION_CODES.ICE_CREAM_SANDWICH等于14。14是Ice Cream Sandwich的API级别,而系统版本是4.0。所以如果你写4.0,你的代码会在从Donut开始的所有设备上执行,因为4是Donut的API级别(Build.VERSION_CODES.DONUT等于4)。

if(Build.VERSION.SDK_INT >= 4.0){
    //this code will be executed on devices running on DONUT (NOT ICS) or later
}

This example is a reason why using 'magic number' is a bad habit.

这个例子是为什么使用“幻数”是一个坏习惯的原因。

回答by Nikita Kurtin

Be aware that Build.VERSION.SDK_INTisn't reliable, it's mentioned by @Falcon165o and recently I ran into that one too.

请注意Build.VERSION.SDK_INT不可靠,@Falcon165o 提到了它,最近我也遇到了那个。

So to get the String data (based on Android version list) of currently installed android, I made a code like this:

所以为了获取当前安装的android的String数据(基于Android版本列表),我编写了这样的代码:

Java

爪哇

//Current Android version data
public static String currentVersion(){
    double release=Double.parseDouble(Build.VERSION.RELEASE.replaceAll("(\d+[.]\d+)(.*)",""));
    String codeName="Unsupported";//below Jelly bean OR above Oreo
    if(release>=4.1 && release<4.4)codeName="Jelly Bean";
    else if(release<5)codeName="Kit Kat";
    else if(release<6)codeName="Lollipop";
    else if(release<7)codeName="Marshmallow";
    else if(release<8)codeName="Nougat";
    else if(release<9)codeName="Oreo";
    return codeName+" v"+release+", API Level: "+Build.VERSION.SDK_INT;
}

Kotlin

科特林

fun currentVersion(): String {
    val release = java.lang.Double.parseDouble(java.lang.String(Build.VERSION.RELEASE).replaceAll("(\d+[.]\d+)(.*)", ""))
    var codeName = "Unsupported"//below Jelly bean OR above Oreo
    if (release >= 4.1 && release < 4.4)  codeName = "Jelly Bean"
    else if (release < 5) codeName = "Kit Kat"
    else if (release < 6) codeName = "Lollipop"
    else if (release < 7) codeName = "Marshmallow"
    else if (release < 8) codeName = "Nougat"
    else if (release < 9) codeName = "Oreo"
    return codeName + " v" + release + ", API Level: " + Build.VERSION.SDK_INT
}

Example of an output it produce:

它产生的输出示例:

Marshmallow v6.0, API Level: 23

Marshmallow v6.0,API 级别:23

回答by HOSHYAR Ahmadpour

For example, a feature only works for api21 up the following we fix bugs in api21 down

例如,一个功能只适用于 api21 up 以下我们修复了 api21 down 中的错误

    if(Build.VERSION.SDK_INT >= 21) {
    //only api 21 above
    }else{
   //only api 21 down
    }

回答by Kunkun Liu

if (Build.VERSION.SDK_INT >= ApiHelper.VERSION_CODES.HONEYCOMB_MR2) {
//do anything you  like.
}