Monday, December 30, 2013

Get the Android version of device

The static final int Build.VERSION.SDK_INT store user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES.

This exercise use Android's SparseArrays to store SDK_INT-Build.VERSION_CODES pairs. SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Objects, both because it avoids auto-boxing keys and its data structure doesn't rely on an extra entry object for each mapping.


Get the Android version of device
Get the Android version of device

package com.example.androidversion;

import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.util.SparseArray;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

 static SparseArray<String> arrayVC = new SparseArray<String>();
    static {
        arrayVC.append(VERSION_CODES.BASE, "The original, first, version of Android.");
        arrayVC.append(VERSION_CODES.BASE_1_1, "First Android update, officially called 1.1");
        arrayVC.append(VERSION_CODES.CUPCAKE, "Android 1.5");
        arrayVC.append(VERSION_CODES.CUR_DEVELOPMENT, "Magic version number...");
        arrayVC.append(VERSION_CODES.DONUT, "Android 1.6");
        arrayVC.append(VERSION_CODES.ECLAIR, "Android 2.0");
        arrayVC.append(VERSION_CODES.ECLAIR_0_1, "Android 2.0.1");
        arrayVC.append(VERSION_CODES.ECLAIR_MR1, "Android 2.1");
        arrayVC.append(VERSION_CODES.FROYO, "Android 2.2");
        arrayVC.append(VERSION_CODES.GINGERBREAD,"Android 2.3");
        arrayVC.append(VERSION_CODES.GINGERBREAD_MR1, "Android 2.3.3");
        arrayVC.append(VERSION_CODES.HONEYCOMB, "Android 3.0");
        arrayVC.append(VERSION_CODES.HONEYCOMB_MR1, "Android 3.1");
        arrayVC.append(VERSION_CODES.HONEYCOMB_MR2, "Android 3.2");
        arrayVC.append(VERSION_CODES.ICE_CREAM_SANDWICH,"Android 4.0");
        arrayVC.append(VERSION_CODES.ICE_CREAM_SANDWICH_MR1,"Android 4.0.3");
        arrayVC.append(VERSION_CODES.JELLY_BEAN, "Android 4.1");
        arrayVC.append(VERSION_CODES.JELLY_BEAN_MR1, "Android 4.2");
        arrayVC.append(VERSION_CODES.JELLY_BEAN_MR2, "Android 4.3");
        arrayVC.append(VERSION_CODES.KITKAT, "Android 4.4");
    }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.activity_main);
  
  TextView textView = new TextView(this);
  setContentView(textView);
  
  int version = Build.VERSION.SDK_INT;
  String BuildVersion = arrayVC.get(version, "unknown");
  
  textView.setText(BuildVersion);
 }

}


No comments: