returns the unique device ID: IMEI for GSM, MEID or ESN for CDMA - Android android.provider

Android examples for android.provider:Settings

Description

returns the unique device ID: IMEI for GSM, MEID or ESN for CDMA

Demo Code

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

public class Main {

  private static Application sApp;

  /**//  www.  j  a v a2  s  . c om
   * returns a unique id for the device: return IMEI if available. if not, return
   * ANDROID_ID from settings provider
   * 
   * @return a unique id
   */
  public static String getDeviceId() {
    String id = getIMEI();
    if (TextUtils.isEmpty(id)) {
      id = android.provider.Settings.Secure.getString(sApp.getContentResolver(),
          android.provider.Settings.Secure.ANDROID_ID);
    }
    return id;
  }

  /**
   * returns the unique device ID: IMEI for GSM, MEID or ESN for CDMA. return null
   * if device ID is not available. Requires Permission: READ_PHONE_STATE
   */
  public static String getIMEI() {
    android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) sApp
        .getSystemService(Context.TELEPHONY_SERVICE);
    // Requires Permission: READ_PHONE_STATE
    String device_id = tm.getDeviceId();
    if (TextUtils.isEmpty(device_id)) {
      device_id = android.provider.Settings.Secure.getString(sApp.getContentResolver(),
          android.provider.Settings.Secure.ANDROID_ID);
    }
    return device_id;
  }

}

Related Tutorials