Android的:如何以编程方式找到android版的名字吗? [英] Android:how to find the android version name programmatically?

查看:119
本文介绍了Android的:如何以编程方式找到android版的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写$ C $下找到Android版本是这样

i write code for find the android version like this

字符串版本= Build.VERSION.RELEASE;

String version=Build.VERSION.RELEASE;

使用此codeI得到的版本号,但我想版本名称。 如何获取版本名称??? /

by using this code i get the version number but i want version name. how to get the version name???/

推荐答案

正如前面所说,反射似乎是关键,这一问题。不需要的StringBuilder和额外的格式,它是只加以说明使用。

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.

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模拟器,我得到这样的输出:

On my 4.1 emulator, I get this output:

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

享受!

这篇关于Android的:如何以编程方式找到android版的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆