尝试在Android 10中检索设备ID和Imei号时,电话管理器将返回null

问题描述

我正在尝试检索设备ID和imei编号以进行用户注册。但是代码将其返回为null。

这是获取设备ID的代码

internal fun getdeviceid(context: Context): String {
    val telephonyManager = context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

    return when {
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> telephonyManager.imei
        else -> telephonyManager.deviceid
    }
}

这是获取imei号码的代码

internal fun getImei1(context: Context): String {
    var imei1 = ""
    if (ActivityCompat.checkSelfPermission(context,Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        imei1 = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> telephonyManager.getImei(0)
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> telephonyManager.getdeviceid(0)
            else -> telephonyManager.deviceid
        }
    } else {
        Toast.makeText(context,"Unable to retrieve IMEI1",Toast.LENGTH_SHORT).show()
    }
    return imei1
}

两者均返回null。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)