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

后端 未结 20 2505
滥情空心
滥情空心 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: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;

    
    

提交回复
热议问题