How to retrieve IMEI from mobile programmatically?

Asked

Viewed 504 times

1

so, I need to recover the IMEI of the phone that is using the app, I tried to use some old methods, but I always get the warning that the method was deprecated...could help me? Grateful from the start!

code used:

final TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

final String imei = telephonyManager.getDeviceId();
  • Related https://answall.com/q/198153/2541

1 answer

2


The method getDeviceId() is deprecated, in fact, as pointed out by documentation, since API 26.

Instead, use the method gethsemane().

final String imei = telephonyManager.getImei();

Not forgetting that the following permission should be added to manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

In addition, as the access permission to the IMEI can be denied by the user at runtime, you should treat this possibility also (Android Studio itself suggests this treatment), otherwise the system will launch a SecurityException:

final TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {               
        //sua lógica aqui
    }

final String imei = telephonyManager.getImei();
  • Stopped the bug. But the app does not open, could help me with the permission, I believe that should be the reason..

  • @Leonardogoulart Reply edited.

  • I put permission and everything, but it keeps giving error... specifically this: java.lang.Securityexception: getImeiForSlot: Neither user 10082 nor Current process has android.permission.READ_PHONE_STATE.

  • @Leonardogoulart, you need to explicitly ask permission then. Take a look in this link and in this here. Both have what you need to ask the user for permissions.

  • Then I managed to fix the error, but it still doesn’t work... giving this error: "Unsupported class: com.mediatek.common.telephony.Ionlyownersimsupport"..

  • You are trying to take a test in the emulator or physical phone? This response suggests installing a specific library in Android Studio and then rebuild the project. See if it helps.

  • Doing both, but focus is on the physical phone. When I use Telephonymanager all Mainactivity textviews disappear...I have Sdk installed and google service tbm

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.