如何检查Android系统版本?

时间:2021-06-21 06:59:41

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

有谁知道我怎样才能程序化地检查系统版本(如1.0、2.2等)?

12 个解决方案

#1


371  

Check android.os.Build.VERSION.

检查android.os.Build.VERSION。

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

#2


688  

Example how to use it:

示例如何使用它:

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

#3


62  

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

构建。版本是获取这些数据的地方。下面是如何格式化它的代码片段。

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)

#4


55  

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 Build.VERSION。无论如何,释放都会起作用!

#5


42  

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

你可以在Build.VERSION中找到Android版本。

The documentation recommends you check Build.VERSION.SDK_INT against 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_INT was 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.SDK instead.

只要您意识到Build.VERSION,这就很好。SDK_INT只在API级别4中引入,即Android 1.6 (Donut)。这不会影响到你,但是如果你想让你的应用在Android 1.5或更早的版本上运行,那么你必须使用不赞成的Build.VERSION。SDK。

#6


26  

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_SANDWICH equals 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.DONUT equals 4).

Build.VERSION_CODES价值。ICE_CREAM_SANDWICH = 14.14是冰淇淋三明治的API级别,而系统版本是4.0。如果你写4.0,你的代码将在所有设备上执行,从Donut开始,因为4是Donut的API级别(Build.VERSION_CODES)。甜甜圈= 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.

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

#7


20  

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 loolipopN for Nougat and so on...

对于ckecking,其他人只需要修改版本代码,比如,kitkat, L for loolipopN for Nougat等等……

#8


9  

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

例如,一个特性只对api21起作用,下面我们将修复api21中的缺陷

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

#9


9  

Be aware that Build.VERSION.SDK_INT isn'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的字符串数据(基于Android版本列表),我做了如下代码:

Java

Java

//Current Android version datapublic static String currentVersion(){    double release=Double.parseDouble(Build.VERSION.RELEASE.replaceAll("(\\d+[.]\\d+)(.*)","$1"));    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+)(.*)", "$1"))    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

棉花糖v6.0, API等级:23

#10


7  

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

#11


3  

use this class

使用这个类

import android.os.Build;/** * Created by MOMANI on 2016/04/14. */public class AndroidVersionUtil {    public static int getApiVersion() {        return android.os.Build.VERSION.SDK_INT;    }    public static boolean isApiVersionGraterOrEqual(int thisVersion) {        return android.os.Build.VERSION.SDK_INT >= thisVersion;    }}

#12


1  

Use This method:

使用这个方法:

 public static String getAndroidVersion() {        String versionName = "";        try {             versionName = String.valueOf(Build.VERSION.RELEASE);        } catch (Exception e) {            e.printStackTrace();        }        return versionName;    }

#1


371  

Check android.os.Build.VERSION.

检查android.os.Build.VERSION。

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

#2


688  

Example how to use it:

示例如何使用它:

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

#3


62  

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

构建。版本是获取这些数据的地方。下面是如何格式化它的代码片段。

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)

#4


55  

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 Build.VERSION。无论如何,释放都会起作用!

#5


42  

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

你可以在Build.VERSION中找到Android版本。

The documentation recommends you check Build.VERSION.SDK_INT against 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_INT was 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.SDK instead.

只要您意识到Build.VERSION,这就很好。SDK_INT只在API级别4中引入,即Android 1.6 (Donut)。这不会影响到你,但是如果你想让你的应用在Android 1.5或更早的版本上运行,那么你必须使用不赞成的Build.VERSION。SDK。

#6


26  

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_SANDWICH equals 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.DONUT equals 4).

Build.VERSION_CODES价值。ICE_CREAM_SANDWICH = 14.14是冰淇淋三明治的API级别,而系统版本是4.0。如果你写4.0,你的代码将在所有设备上执行,从Donut开始,因为4是Donut的API级别(Build.VERSION_CODES)。甜甜圈= 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.

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

#7


20  

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 loolipopN for Nougat and so on...

对于ckecking,其他人只需要修改版本代码,比如,kitkat, L for loolipopN for Nougat等等……

#8


9  

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

例如,一个特性只对api21起作用,下面我们将修复api21中的缺陷

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

#9


9  

Be aware that Build.VERSION.SDK_INT isn'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的字符串数据(基于Android版本列表),我做了如下代码:

Java

Java

//Current Android version datapublic static String currentVersion(){    double release=Double.parseDouble(Build.VERSION.RELEASE.replaceAll("(\\d+[.]\\d+)(.*)","$1"));    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+)(.*)", "$1"))    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

棉花糖v6.0, API等级:23

#10


7  

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

#11


3  

use this class

使用这个类

import android.os.Build;/** * Created by MOMANI on 2016/04/14. */public class AndroidVersionUtil {    public static int getApiVersion() {        return android.os.Build.VERSION.SDK_INT;    }    public static boolean isApiVersionGraterOrEqual(int thisVersion) {        return android.os.Build.VERSION.SDK_INT >= thisVersion;    }}

#12


1  

Use This method:

使用这个方法:

 public static String getAndroidVersion() {        String versionName = "";        try {             versionName = String.valueOf(Build.VERSION.RELEASE);        } catch (Exception e) {            e.printStackTrace();        }        return versionName;    }