How to get the device's IMEI/ESN programmatically in android?

后端 未结 20 2283
滥情空心
滥情空心 2020-11-22 05:19

To identify each devices uniquely I would like to use the IMEI (or ESN number for CDMA devices). How to access this programmatically?

相关标签:
20条回答
  • 2020-11-22 05:52

    As in API 26 getDeviceId() is depreciated so you can use following code to cater API 26 and earlier versions

    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String imei="";
    if (android.os.Build.VERSION.SDK_INT >= 26) {
      imei=telephonyManager.getImei();
    }
    else
    {
      imei=telephonyManager.getDeviceId();
    }
    

    Don't forget to add permission requests for READ_PHONE_STATE to use the above code.

    UPDATE: From Android 10 its is restricted for user apps to get non-resettable hardware identifiers like IMEI.

    0 讨论(0)
  • 2020-11-22 05:52

    for API Level 11 or above:

    case TelephonyManager.PHONE_TYPE_SIP: 
    return "SIP";
    
    TelephonyManager tm= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    textDeviceID.setText(getDeviceID(tm));
    
    0 讨论(0)
  • 2020-11-22 05:59

    For those looking for a Kotlin version, you can use something like this;

    private fun telephonyService() {
        val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
        val imei = if (android.os.Build.VERSION.SDK_INT >= 26) {
            Timber.i("Phone >= 26 IMEI")
            telephonyManager.imei
        } else {
            Timber.i("Phone IMEI < 26")
            telephonyManager.deviceId
        }
    
        Timber.i("Phone IMEI $imei")
    }
    

    NOTE: You must wrap telephonyService() above with a permission check using checkSelfPermission or whatever method you use.

    Also add this permission in the manifest file;

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    
    0 讨论(0)
  • 2020-11-22 06:00

    The method getDeviceId() is deprecated. There a new method for this getImei(int)

    Check here

    0 讨论(0)
  • 2020-11-22 06:02

    Or you can use the ANDROID_ID setting from Android.Provider.Settings.System (as described here strazerre.com).

    This has the advantage that it doesn't require special permissions but can change if another application has write access and changes it (which is apparently unusual but not impossible).

    Just for reference here is the code from the blog:

    import android.provider.Settings;
    import android.provider.Settings.System;   
    
    String androidID = System.getString(this.getContentResolver(),Secure.ANDROID_ID);
    

    Implementation note: if the ID is critical to the system architecture you need to be aware that in practice some of the very low end Android phones & tablets have been found reusing the same ANDROID_ID (9774d56d682e549c was the value showing up in our logs)

    0 讨论(0)
  • 2020-11-22 06:04

    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.

    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> 
    

    Permission Required READ_PHONE_STATE in manifest file.

    0 讨论(0)
提交回复
热议问题