android.telephony.TelephonyManager.getDeviceId()
is not working after migrating to API level 29
(Android 10), as it's throwing SecurityException
. Please anyone can help out me to get a unique device id.
As per the latest release in Android 10, Restriction on non-resettable device identifiers. PPS must have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access the device's non-resettable identifiers, which include both IMEI and serial number .
"READ_PRIVILEGE_PHONE_STATE"
is only accessible by The best practices suggest that you should "Avoid using hardware identifiers." for unique identifiers. You can use an instance id from firebase eg FirebaseInstanceId.getInstance().getId();
.
Or you can go with this also,
String deviceId = android.provider.Settings.Secure.getString(
context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
Use the below code:
public String androidId;
androidId = String
.format("%16s", Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID))
.replace(' ', '0');
You can use below which is preferrable which has the least chances of reset.
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
Please refer THIS from which you can decide the preferable one.
getDeviceId is deprecated you can use this method to get the device IMEI
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
IMEINumber = telephonyMgr.getImei();
} else {
IMEINumber = telephonyMgr.getDeviceId();
}
set this permission in manifeast:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Now use this to get unique no:
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
If your project contains firebase. use this --> FirebaseInstanceId.getInstance().getToken()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.