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();