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

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

    Kotlin Code for getting DeviceId ( IMEI ) with handling permission & comparability check for all android versions :

     val  telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
            == PackageManager.PERMISSION_GRANTED) {
            // Permission is  granted
            val imei : String? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)  telephonyManager.imei
            // older OS  versions
            else  telephonyManager.deviceId
    
            imei?.let {
                Log.i("Log", "DeviceId=$it" )
            }
    
        } else {  // Permission is not granted
    
        }
    

    Also add this permission to AndroidManifest.xml :

     
    

提交回复
热议问题