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

后端 未结 20 2516
滥情空心
滥情空心 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.

提交回复
热议问题