我编写代码来查找像这样的Android版本
String version=Build.VERSION.RELEASE;
通过使用此代码,我得到版本号,但我想要版本名称。 如何获取版本名称?
答案 0 :(得分:50)
如前所述,反思似乎是这个问题的关键。 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());
在我的4.1模拟器上,我得到了这个输出:
D/MainActivity( 1551): OS: android : 4.1.1 : JELLY_BEAN : sdk=16
享受!
答案 1 :(得分:12)
优化代码,这将有效:
use Config
答案 2 :(得分:5)
http://developer.android.com/reference/android/os/Build.VERSION_CODES.html包含具有您正在寻找的名称的字段。 因此,您可以使用反射来查找哪个字段对应于“版本”值。
为什么要这个名字?
答案 3 :(得分:3)
您将从这些
获取信息android.os.Build.VERSION_CODES
android.os.Build.VERSION.SDK_INT
可以从此链接获得更多信息:
Retrieving Android API version programmatically
希望这会对你有所帮助。
答案 4 :(得分:3)
检查出来:
// 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)
请注意,此解决方案仅在版本代码增加1时才有效,并且您必须使用每个新的Android版本更新列表(如果准确的话很重要)。
答案 5 :(得分:1)
如android文档中所述,手机运行的SDK级别(整数)可在以下位置找到:
android.os.Build.VERSION.SDK_INT;
与此int对应的枚举位于android.os.Build.VERSION_CODES类中。
代码示例:
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
}
编辑:此SDK_INT自Donut(Android 1.6 / API4)开始提供,因此当您使用它时,请确保您的应用程序与Cupcake(Android 1.5 / API3)不兼容,否则您的应用程序将崩溃(感谢Programmer Bruce精度)。
答案 6 :(得分:1)
在 API 28(Android Pie)之后, Build.VERSION_CODES 已更改某些字段。
因此,如果您使用:
Field[] fields = Build.VERSION_CODES.class.getFields();
String osName = fields[Build.VERSION.SDK_INT + 1].getName();
由于超出范围异常,将立即导致您的应用崩溃。
所有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();
}
}
或者使用Java 8风格的Kotlin:
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 }
答案 7 :(得分:0)
当我尝试在API 19上运行模拟器时,反射无效。
所以我想出了一个混合答案&#34; Nacho Coloma&#34;和凯文格兰特&#34;
String osName="";
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) {
osName=fieldName; break;
}
}
if(osName.isEmpty()) {
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",
"JELLY_BEAN", "JELLY_BEAN", "KITKAT", "KITKAT",
"LOLLIPOOP", "LOLLIPOOP_MR1", "MARSHMALLOW", "NOUGAT",
"NOUGAT", "OREO", "OREO", "ANDROID P"};
int index = Build.VERSION.SDK_INT - 1;
osName = index < mapper.length ? mapper[index] : "UNKNOWN_VERSION";
}
答案 8 :(得分:0)
此答案为棉花糖提供了更好的(全名)名称,并且如果您在新版本发布时不更新应用程序(两全其美),还可以保证未来的安全。版本名称列表来自here。
String versionName =
Build.VERSION_CODES.class.getFields()[Build.VERSION.SDK_INT + 1].getName();
String[] versionNames = 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", "JELLY_BEAN_MR1", "JELLY_BEAN_MR2", "KITKAT", "KITKAT_WATCH",
"LOLLIPOP", "LOLLIPOP_MR1", "MARSHMALLOW", "NOUGAT", "OREO", "OREO_MR1"
};
int nameIndex = Build.VERSION.SDK_INT - 1;
if (nameIndex < versionNames.length) {
versionName = versionNames[nameIndex];
}
很明显,这是其他答案的衍生。根据我的经验,当您尝试编辑现有答案以改善答案时,人们会不喜欢它,因此我创建了一个新答案。
答案 9 :(得分:0)
我已经应用了上面的解决方案,但是对于版本名称中的“ 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;
}
答案 10 :(得分:0)
Build.VERSION.SDK;是返回正确的结果,但从API 16开始不推荐使用SDK:Android 4.1(Jelly Bean)。因此它将显示为w
答案 11 :(得分:0)
如果您要寻找Xamarin.Android
,解决方案很简单:
Build.VERSION.SdkInt.ToString();
非常简单。
原因是SdkInt
映射到Android的SDK_INT
:
[Register("SDK_INT", ApiSince = 4)]
public static BuildVersionCodes SdkInt
{
get
{
return (BuildVersionCodes) Build.VERSION._members.StaticFields.GetInt32Value("SDK_INT.I");
}
}
哪个映射到一个枚举,并且在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,
...
答案 12 :(得分:0)
入门级友好解决方案。 我使用版本代码,因为它们比代码名称更好,更容易混淆。对于前。软糖,奥利奥,棒棒糖等在单个代号下具有不同的版本代号。 无论如何,您都可以用代号替换版本代码。
var androidVersion = ""
androidVersion = when (android.os.Build.VERSION.SDK_INT){
16 -> "Android 4.1"
17 -> "Android 4.2"
18 -> "Android 4.3"
19 -> "Android 4.4"
21 -> "Android 5.0"
22 -> "Android 5.1"
23 -> "Android 6.0"
24 -> "Android 7.0"
25 -> "Android 7.1"
26 -> "Android 8.0"
27 -> "Android 8.1"
28 -> "Android 9"
29 -> "Android 10"
else -> "API " + android.os.Build.VERSION.SDK_INT
}
答案 13 :(得分:0)
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 28) {
// Do something for 28 and above versions
} else {
// do something for phones running an SDK before 28
}
并使用此表:
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 9 Pie
29 Q Android 10
10000 CUR_DEVELOPMENT Current Development Version
答案 14 :(得分:-2)
您可以尝试以下功能。
public String AndroidOSName() {
String os = Build.VERSION.SDK;
System.out.println("here is my os" + " " + os);
if (os.equals("23")) {
return "Marshmallow";
} else if (os.equals("21")) {
return "Lollipop";
} else if (os.equals("22")) {
return "LOLLIPOP_MR1";
} else if (os.equals("20")) {
return "KitKat";
} else if (os.equals("19")) {
return "KitKat";
} else if (os.equals("18")) {
return "Jelly Bean";
} else if (os.equals("17")) {
return "Jelly Bean";
} else if (os.equals("16")) {
return "Jelly Bean";
} else if (os.equals("15")) {
return "Ice Cream Sandwich";
} else if (os.equals("14")) {
return "Ice Cream Sandwich";
} else if (os.equals("13")) {
return "Honeycomb";
} else if (os.equals("12")) {
return "Honeycomb";
} else if (os.equals("11")) {
return "Honeycomb";
} else if (os.equals("10")) {
return "Gingerbread";
} else if (os.equals("9")) {
return "Froyo";
} else if (os.equals("8")) {
return "Froyo";
} else {
return "Not Found";
}
}