Jun 2, 2011

Get unique device ID (IMEI, MEID or ESN) of Android phones.

The method getDeviceId() of TelephonyManager returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return null if device ID is not available. Permission READ_PHONE_STATE is required.

Get unique device ID (IMEI, MEID or ESN) of Android phones.

package com.AndroidTelephonyManager;

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

public class AndroidTelephonyManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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;
}

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/deviceid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


The returned unique device ID is not the randomly generted 64-bit number, ANDROID_ID. For ANDROID_ID refer to the post: Get ANDROID_ID

Related Post:
- Get Network info using TelephonyManager

3 comments:

  1. It would be super education if you were to provide a non-internets apk for displaying one's own MEID/IMEI

    ReplyDelete
  2. I do not know what I am doing wrong, every time I put in these settings, the app force closes. I take these out, and it works fine. I am stumped.

    ReplyDelete
  3. @Duck
    Add this line in your applicationManifest.xml just above the

    If you are testing on virtual device, it will show 000000000000000.

    ReplyDelete

Infolinks In Text Ads