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();
Related https://answall.com/q/198153/2541
– ramaral