How to obtain IMEI / ESN of device programmatically in android?

In order to uniquely identify each device, I want to use IMEI (or ESN number of CDMA device). How can I access it programmatically?

#1 building

Come from: http : //mytechead.wordpress.com/2011/08/28/how-to-get-imei-number-of-android-device/ :

The following code helps you get the IMEI number of your Android device:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String device_id = tm.getDeviceId();

Required permissions in Android Manifest:

android.permission.READ_PHONE_STATE

Note: IMEI will be empty if the tablet or device cannot act as a mobile phone.

#2 building

When the device does not have a phone function, I use the following code to obtain IMEI or use secure.android? ID as an alternative:

/**
 * Returns the unique identifier for the device
 *
 * @return unique identifier for the device
 */
public String getDeviceIMEI() {
    String deviceUniqueIdentifier = null;
    TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    if (null != tm) {
        deviceUniqueIdentifier = tm.getDeviceId();
    }
    if (null == deviceUniqueIdentifier || 0 == deviceUniqueIdentifier.length()) {
        deviceUniqueIdentifier = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
    }
    return deviceUniqueIdentifier;
}

#3 building

API grade 11 or above:

case TelephonyManager.PHONE_TYPE_SIP: 
return "SIP";

TelephonyManager tm= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
textDeviceID.setText(getDeviceID(tm));

#4 building

The getDeviceId() method of the TelephonyManager returns a unique device ID, such as IMEI for GSM and MEID or ESN for CDMA mobile phones. Returns null if the device ID is not available.

Java code

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

<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello">
<textview android:id="@+id/deviceid" android:layout_height="wrap_content" android:layout_width="fill_parent">
</textview></textview></linearlayout> 

The permission read? Phone? State in the manifest file.

#5 building

You want to call android.telephony.TelephonyManager.getDeviceId() .

This returns a string that uniquely identifies the device (IMEI on GSM, MEID on CDMA).

You need the following permissions for Android manifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

For this reason.

That said, be careful. Users not only want to know why your application wants to access its phone stack, but if they get a new device, it can be difficult to migrate data.

Update: as described in the comments below, this is not a secure way to authenticate users and raises privacy issues. Not recommended. Instead, if you want to implement a frictionless login system, check Google + login API .

If you only want to use a lightweight method to retain a string when the user resets the phone (or buys a new device), then Android backup API Also available.

Tags: Android Mobile xml Java

Posted on Wed, 18 Mar 2020 08:36:11 -0400 by caminator