Showing posts with label How to get IMEI or ESN Programmatically in Android. Show all posts
Showing posts with label How to get IMEI or ESN Programmatically in Android. Show all posts

Wednesday, August 13, 2014

How to get IMEI or ESN Programmatically in Android

To Get IMEI or ESN Programmatically in Android


package mn.androidms_imei;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        TextView textDeviceID = (TextView)findViewById(R.id.deviceid);

        //retrieve a reference to an instance of TelephonyManager
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        textDeviceID.setText(getDeviceID(telephonyManager));

    }

    String getDeviceID(TelephonyManager phonyManager){

     String id = phonyManager.getDeviceId();
     if (id == null){
      id = "not available";
     }

     int phoneType = phonyManager.getPhoneType();
     switch(phoneType){
     case TelephonyManager.PHONE_TYPE_NONE:
      return "NONE: " + id;

     case TelephonyManager.PHONE_TYPE_GSM:
      return "GSM: IMEI=" + id;

     case TelephonyManager.PHONE_TYPE_CDMA:
      return "CDMA: MEID/ESN=" + id;

     /*
      *  for API Level 11 or above
      *  case TelephonyManager.PHONE_TYPE_SIP:
      *   return "SIP";
      */


     default:
      return "UNKNOWN: ID=" + id;
     }

    }
   
}

//***activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/deviceid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

//***Add Permission 

android.permission.READ_PHONE_STATE