Question

In my application I want to show the Android OS code name of the device. i.e. I want to get the android OS code names like Honeycomb or Gingerbread pragmatically at run time from device.

Is there any way to get that codename sting itself? I want’s to show that name in one of the screens in my app.

I can compare like this and hardcode the string.

 if (Build.VERSION.SDK_INT==Build.VERSION_CODES.GINGERBREAD) {

    CodeName=”GINGERBREAD”

 } else if (Build.VERSION.SDK_INT==Build.VERSION_CODES.HONEYCOMB){

    CodeName=”Honeycom”
 }

But if tomorrow new version released then I’ve to change the code again. That’s why I am looking for some why to get the code name string itself instead of getting version number.

So can anyone suggest some way to get the device's OS code name like we get the OS version number?

Was it helpful?

Solution

There is no API in Android documentation to return the String of code name for you. You can obtain only VERSION NUMBER like 2.3, which you can show to users.

OTHER TIPS

Try this.

enum Codenames
{
    BASE, BASE_1,
    CUPCAKE, 
    DONUT, 
    ECLAIR, ECLAIR_MR1, ECLAIR_MR2, 
    FROYO,
    GINGERBREAD, GINGERBREAD_MR1, 
    HONEYCOMB, HONEYCOMB_MR1, HONEYCOMB_MR2,
    ICE_CREAM_SANDWICH, ICE_CREAM_SANDWICH_MR1,
    JELLY_BEAN;

    public static Codenames getCodename()
    {
        int api = Build.VERSION.SDK_INT;

        switch (api) {
        case 1:

            return BASE;
        case 2:

            return BASE_1;
        case 3:

            return CUPCAKE;
        case 4:

            return DONUT;
        case 5:

            return ECLAIR;
        case 6:

            return ECLAIR_MR1;
        case 7:

            return ECLAIR_MR2;
        case 8:

            return FROYO;
        case 9:

            return GINGERBREAD;
        case 10:

            return GINGERBREAD_MR1;
        case 11:

            return HONEYCOMB;
        case 12:

            return HONEYCOMB_MR1;
        case 13:

            return HONEYCOMB_MR2;
        case 14:

            return ICE_CREAM_SANDWICH;
        case 15:

            return ICE_CREAM_SANDWICH_MR1;
        case 16:

            return JELLY_BEAN;
        default:
            return null;
        }
    }

};

and then do

Codenames c = Codenames.getCodename();

Why not use reflection ?

    String versionCodename = null;
    Field[] fields = Build.VERSION_CODES.class.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field fld = fields[i];
        try {
            int val = fld.getInt(null);
            if (val == Build.VERSION.SDK_INT && fld.getName().length() > 1) {
                String fldName = fld.getName();
                versionCodename = fldName.substring(0, 1).toUpperCase() + fldName.substring(1).toLowerCase();
                break;
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

import java.lang.reflect.Field;

  Field[] fields = Build.VERSION_CODES.class.getFields();

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

  Log.d("OsName",""+osName);

Like this,simply change the .GINGERBREAD to whatever you want, ie. .HONEYCOMB

if (Build.VERSION.SDK_INT<Build.VERSION_CODES.GINGERBREAD) {
     // do code for device before gingerbread
} 

Also check here for more info - http://developer.android.com/reference/android/os/Build.VERSION.html

Edit - to get the version number as an int, you do

int versionNumber = Build.VERSION.SDK_INT;

And then you can use

if (versionNumber == 2.3){
    //do code here
}

the deprecated String Build.VERSION.SDK should work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top