如何以编程方式查找Android版本名称?

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

How to find the Android version name programmatically?

androidandroid-ndk

提问by Venkat

I write code for finding the Android version like this

我编写代码来查找这样的 Android 版本

String version=Build.VERSION.RELEASE;

by using this code I am get the version number but I want version name. how to get the version name?

通过使用此代码,我获得了版本号,但我想要版本名称。如何获取版本名称?

回答by Kevin Grant

As suggested earlier, reflection seems to be the key to this question. The StringBuilder and extra formatting is not required, it was added only to illustrate usage.

如前所述,反思似乎是解决这个问题的关键。StringBuilder 和额外的格式不是必需的,添加它只是为了说明用法。

import java.lang.reflect.Field;
...

StringBuilder builder = new StringBuilder();
builder.append("android : ").append(Build.VERSION.RELEASE);

Field[] fields = Build.VERSION_CODES.class.getFields();
for (Field field : fields) {
    String fieldName = field.getName();
    int fieldValue = -1;

    try {
        fieldValue = field.getInt(new Object());
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    if (fieldValue == Build.VERSION.SDK_INT) {
        builder.append(" : ").append(fieldName).append(" : ");
        builder.append("sdk=").append(fieldValue);
    }
}

Log.d(LOG_TAG, "OS: " + builder.toString());

On my 4.1 emulator, I get this output:

在我的 4.1 模拟器上,我得到以下输出:

D/MainActivity( 1551): OS: android : 4.1.1 : JELLY_BEAN : sdk=16

Enjoy!

享受!

回答by Shyam Kumar

Optimized code, this will work:

优化代码,这将起作用:

import java.lang.reflect.Field;

Field[] fields = Build.VERSION_CODES.class.getFields();
String osName = fields[Build.VERSION.SDK_INT + 1].getName();
Log.d("Android OsName:",osName);

回答by Tr?n Leo

After API 28 (Android Pie), Build.VERSION_CODESwere changedsome fields.

经过API 28(安卓派)Build.VERSION_CODES改变了某些字段。

So, if you using:

因此,如果您使用:

Field[] fields = Build.VERSION_CODES.class.getFields();
String osName = fields[Build.VERSION.SDK_INT + 1].getName();

will cause your app crash immediately because of Out Of Range Exception.

由于超出范围异常,将立即导致您的应用程序崩溃。

The solution for all API level is:

所有 API 级别的解决方案是:

Field[] fields = Build.VERSION_CODES.class.getFields();
String codeName = "UNKNOWN";
for (Field field : fields) {
    try {
        if (field.getInt(Build.VERSION_CODES.class) == Build.VERSION.SDK_INT) {
            codeName = field.getName();
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

Or in Kotlin with Java 8's style:

或者在 Kotlin 中使用 Java 8 的风格:

val fields = Build.VERSION_CODES::class.java.fields
var codeName = "UNKNOWN"
fields.filter { it.getInt(Build.VERSION_CODES::class) == Build.VERSION.SDK_INT }
      .forEach { codeName = it.name }

回答by Philippe Girolami

http://developer.android.com/reference/android/os/Build.VERSION_CODES.htmlcontains fields that have the name you're looking for. So you could use reflexion to find which field corresponds to the "version" value.

http://developer.android.com/reference/android/os/Build.VERSION_CODES.html包含具有您要查找的名称的字段。因此,您可以使用反射来查找对应于“版本”值的字段。

Why do you want the name ?

你为什么要这个名字?

回答by UVM

You will get the information from these

您将获得这些信息

android.os.Build.VERSION_CODES 

android.os.Build.VERSION.SDK_INT

More information can be had from this link:

可以从此链接获得更多信息:

Retrieving Android API version programmatically

以编程方式检索 Android API 版本

Hope this will help you.

希望这会帮助你。

回答by Nacho Coloma

Check this out:

看一下这个:

// Names taken from android.os.build.VERSION_CODES
String[] mapper = new String[] {
    "ANDROID BASE", "ANDROID BASE 1.1", "CUPCAKE", "DONUT",
    "ECLAIR", "ECLAIR_0_1", "ECLAIR_MR1", "FROYO", "GINGERBREAD",
    "GINGERBREAD_MR1", "HONEYCOMB", "HONEYCOMB_MR1", "HONEYCOMB_MR2",
    "ICE_CREAM_SANDWICH", "ICE_CREAM_SANDWICH_MR1", "JELLY_BEAN"
};
int index = Build.VERSION.SDK_INT - 1;
String versionName = index < mapper.length? mapper[index] : "UNKNOWN_VERSION"; // > JELLY_BEAN)

Be aware that this solution will only work as far as the version codes keep being incremented by one, and you will have to update the list with each new android version (if being accurate is important).

请注意,此解决方案仅在版本代码不断增加 1 时才有效,并且您必须使用每个新的 android 版本更新列表(如果准确很重要)。

回答by hitesh141

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 enum corresponding to this int is in the android.os.Build.VERSION_CODES class.

与此 int 对应的枚举位于 android.os.Build.VERSION_CODES 类中。

Code example:

代码示例:

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

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 提供的精确度) .

回答by RexSplode

I've applied the solution above, but wasn't quite happy with "O" and "N" as version names, so I made some changes. Hope it will be useful for others too

我已经应用了上面的解决方案,但对“O”和“N”作为版本名称不太满意,所以我做了一些更改。希望它对其他人也有用

    /**
 * Gets the version name from version code. Note! Needs to be updated
 * when new versions arrive, or will return a single letter. Like Android 8.0 - Oreo
 * yields "O" as a version name.
 * @return version name of device's OS
 */
private static String getOsVersionName() {
    Field[] fields = Build.VERSION_CODES.class.getFields();
    String name =  fields[Build.VERSION.SDK_INT + 1].getName();

    if(name.equals("O")) name = "Oreo";
    if(name.equals("N")) name = "Nougat";
    if(name.equals("M")) name = "Marshmallow";

    if(name.startsWith("O_")) name = "Oreo++";
    if(name.startsWith("N_")) name = "Nougat++";

    return name;
}

回答by shiv om Bhardwaj

Build.VERSION.SDK; is return correct result but SDK is deprecated as of API 16: Android 4.1 (Jelly Bean).so it will show like as w

构建.版本.SDK; 返回正确的结果,但 SDK 从 API 16: Android 4.1 (Jelly Bean) 开始被弃用。所以它会显示为 w

回答by Bruno Garcia

If you're looking for Xamarin.Androidthe solution is simply:

如果您正在寻找Xamarin.Android解决方案,只需:

Build.VERSION.SdkInt.ToString();

Pretty simple.

很简单。

The reason is SdkIntis mapped to Android's SDK_INT:

原因是SdkInt映射到 Android 的SDK_INT

[Register("SDK_INT", ApiSince = 4)]
public static BuildVersionCodes SdkInt
{
  get
  {
    return (BuildVersionCodes) Build.VERSION._members.StaticFields.GetInt32Value("SDK_INT.I");
  }
}

Which maps to an enum, and in C# when an enum ToStringis called, the name (as opposed to the value) of the enum is returned instead:

它映射到枚举,在 C#ToString中调用枚举时,将返回枚举的名称(而不是值):

 public enum BuildVersionCodes
  {
    [IntDefinition("Android.OS.Build.VERSION_CODES.Base", JniField = "android/os/Build$VERSION_CODES.BASE")] Base = 1,
    [IntDefinition("Android.OS.Build.VERSION_CODES.Base11", JniField = "android/os/Build$VERSION_CODES.BASE_1_1")] Base11 = 2,
    [IntDefinition("Android.OS.Build.VERSION_CODES.Cupcake", JniField = "android/os/Build$VERSION_CODES.CUPCAKE")] Cupcake = 3,
 ...