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

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

    For Android 10 Following code is working for me.

    val uid: String = Settings.Secure.getString(ctx.applicationContext.contentResolver, Settings.Secure.ANDROID_ID)
    if (ContextCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                imei = when {
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> {
                        uid
                    }
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
                        telephonyManager.imei
                    }
                    else -> {
                        telephonyManager.deviceId
                    }
                }
            }
    
    

提交回复
热议问题