2

It's fairly simple and straight forward to get the current devices API level using:

Build.VERSION.SDK_INT

and using that it's also fairly easy to get the version name as of 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 "";
}

The problem is this gives values such as:

JELLY_BEAN_MR1
HONEYCOMB_MR2

Now, I could just manually add a list of OS version strings myself - which would be fine but once we get past API 22, I would have to update the product simply to add in some extra strings.

My brain tells me there must be an internal field which displays this value somewhere in the OS but I'm finding it decidedly difficult to find where this is.

Any help would be useful

4
  • ... use a switch case? Like: switch(Build.VERSION.SDK_INT){case 1:return "First Android Version ever!";break;} Apr 28, 2015 at 10:23
  • Over what data set? I'm looking for the data set which includes human readable values which should be available somewhere on the device.
    – Graeme
    Apr 28, 2015 at 10:24
  • I don't think there is such a table anywhere (apart the one which returns "JELLY_BEAN_MR1"). Therefore, you get MR1 and such. By the way, you could simply replace "_" with " " and "MR" with "Maintenance Release ". So, you'd get "JELLY BEAN Maintenance Release 1". Even better, if you proper-case the strings (first char of every word uppercase, the rest lowercase), so to finally get "Jelly Bean Maintenance Release 1". I can't think of anything simpler and nicer. Apr 28, 2015 at 10:30
  • Yeah, that has always been my fallback position (I'll post as an answer now) but I don't want to have to resort to string manipulation which relies on Google keeping their naming convention (which, granted, is pretty likely at this stage)
    – Graeme
    Apr 28, 2015 at 10:34

1 Answer 1

1

This is a hack so I don't really want to use it. It does however manage to use regexing to pull out some pretty reasonable display values.

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 };
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.