1

I a beginner in android development and I try to get device IMEI but I have a problem of permission.

I don't have android.permission.READ_PRIVILEGED_PHONE_STATE property in my project but the function getImei() of TelephonyManger.java require this permission.

How can I resolve this? I am a little lost.

  • 1
    In short you can't as android has elevated the permission model regarding device hardware id your has to be the device owner/profile ower to get the permission to get device IMEI. https://developer.android.com/about/versions/10/privacy/changes#non-resettable-device-ids – Piyush Jul 12 '22 at 09:28
  • Yes I could see that, but we don't have a solution to get this new permission ? What does it mean, "be the device owner/profile owner" ? – Thomas Dulcamara Jul 12 '22 at 09:33
  • @Piyush if you have a solution (i think i'm dreaming xD) – Thomas Dulcamara Jul 12 '22 at 09:37
  • 1
    No, you can't give the permission, The `READ_PRIVILEGED_PHONE_STATE` permission is only granted to apps signed with the platform key and privileged system apps. - – Nitish Jul 12 '22 at 09:38
  • Thanks @Nitish, so we can't get the IMEI... We have to ask the user for this... – Thomas Dulcamara Jul 12 '22 at 09:41
  • 1
    I don't think any user will be willing to share such info. It's better to look for alternative - [Beast practice for unquies ids](https://developer.android.com/training/articles/user-data-ids) , [no-imei-for-android-developers-in-android-10](https://stackoverflow.com/questions/57993401/no-imei-for-android-developers-in-android-10) – Nitish Jul 12 '22 at 09:44

2 Answers2

0

try use below code in kotlin.

val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as
     TelephonyManager
     if (ActivityCompat.checkSelfPermission(this@MainActivity,
     Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this@MainActivity,
        arrayOf(Manifest.permission.READ_PHONE_STATE), REQUEST_CODE)
        return@setOnClickListener
     }  
     IMEINumber = telephonyManager.deviceId
     

this link would be helpful. How to get the device's IMEI/ESN programmatically in android?

anddevmanu
  • 1,459
  • 14
  • 19
0

The answer is sad. We can't !

Like @Nitsh said, "The READ_PRIVILEGED_PHONE_STATE permission is only granted to apps signed with the platform key and privileged system apps".