tm.getDeviceId() is deprecated?

前端 未结 6 841
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 09:25

I\'m getting the IMEI and device Id\'s, so here I am getting a problem getDeviceId() is deprecated.

TelephonyManager tm = (Telephon         


        
6条回答
  •  忘掉有多难
    2020-11-29 10:04

    Kotlin Code for getting DeviceId ( IMEI ) with handling with permission & compatibility 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
             } else { // older OS  versions
                 telephonyManager.getDeviceId()
             }
    
            imei?.let {
                Log.i("Log", "DeviceId=$imei" )
            }
    
        } else {  // Permission is not granted
    
        }
    

    Also add this permission to AndroidManifest.xml :

     
    

提交回复
热议问题