How to recover the IMEI from the cell phone programmatically?

1

So, I need to recover the IMEI from the mobile that is using the app, I tried to use some old methods, but I always get the warning that the method has been deprecated ... could they help me? Thank you in advance!

code used:

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

final String imei = telephonyManager.getDeviceId();
    
asked by anonymous 12.06.2018 / 19:36

1 answer

2

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

Instead, use the getImei () method.

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, because IMEI access permission can be denied by the user at run time, you may want to treat this possibility too (Android Studio itself suggests this), 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();
    
12.06.2018 / 19:54