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.

- 9,724
- 11
- 63
- 92

- 11
- 1
- 1
- 1
-
1Does this answer your question? [In Android 10 getDeviceID value is null](https://stackoverflow.com/questions/58022573/in-android-10-getdeviceid-value-is-null) – Srikar Reddy Jan 08 '20 at 06:43
6 Answers
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 e.g 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);

- 2,882
- 2
- 24
- 35
Use the below code:
public String androidId;
androidId = String
.format("%16s", Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID))
.replace(' ', '0');

- 2,534
- 2
- 13
- 20
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.

- 1,869
- 1
- 10
- 23
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();

- 1,972
- 1
- 8
- 17
If your project contains firebase. use this --> FirebaseInstanceId.getInstance().getToken()

- 16
- 4
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();
}

- 719
- 7
- 12