android - 如何获得人类可读的 Android 操作系统版本

标签 android internationalization

使用以下方法获取当前设备 API 级别相当简单直接:

Build.VERSION.SDK_INT

并且使用它也很容易获得 Build.VERSION_CODES

的版本名称
public static String getDisplayOS() {
    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) {
            return fieldName;
        }
    }
    return "";
}

问题是这会给出如下值:

JELLY_BEAN_MR1
HONEYCOMB_MR2

现在,我可以自己手动添加操作系统版本字符串列表 - 这很好,但一旦我们通过了 API 22,我将不得不更新产品以添加一些额外的字符串。

我的大脑告诉我一定有一个内部字段在操作系统中某处显示这个值,但我发现很难找到它在哪里。

任何帮助都是有用的

最佳答案

这是一个 hack,所以我真的不想使用它。然而,它确实设法使用正则表达式来提取一些非常合理的显示值。

public static String[] getDisplayOS() {
    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) {          
            fieldName = fieldName.replaceAll("_", " ");
            String firstLetter = fieldName.substring(0, 1);
            fieldName = firstLetter.toUpperCase() + fieldName.substring(1).toLowerCase();

             Pattern p = Pattern.compile(" [a-z]");
             Matcher m = p.matcher(fieldName);
             while (m.find()) {
                 int index = m.start();
                 fieldName = fieldName.substring(0, index) + fieldName.substring(index, index+2).toUpperCase() + fieldName.substring(index+2);
             }

             Pattern mrPattern = Pattern.compile(" (Mr\\d)");
             Matcher mrMatcher = mrPattern.matcher(fieldName);
             if (mrMatcher.find()) {
                 fieldName = fieldName.replaceAll(" Mr\\d", "");                 
                 return new String[] { fieldName, mrMatcher.group(1).toUpperCase() };
             }
             return new String[] { fieldName, null };
        }
    }
    return new String[] { null, null };
}

关于android - 如何获得人类可读的 Android 操作系统版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29916018/

相关文章:

android - 使用一次<android.support.design.widget时我的应用程序崩溃

ruby-on-rails - Rails 3本地化路线并显示资源

java - 如何根据创建时间对 Firebase 数据进行排序?

android - android 如何让用户不需要再次登录

ruby-on-rails - 浏览器语言设置 es-419 的 Rails 奇怪行为

python - 在 {% if .....%} 标签内添加 {% trans ""%} 标签

c++ - 重写 MFC 应用程序的 LoadString

reactjs - React-i18next addResourceBundle 不是一个函数

android - Nexus 7 Android Java VideoView,无法播放此视频

android - 动画结束时如何删除 View ?