I have already read documentation and I get it that IMEI, Android ID, SECURE ID etc has a restriction now. Then I have used UUID to use in my apps as a unique ID but the problem is that while the storage or data are cleared UUID which is stored in sharedpreferences are also removed and get a different UUID. Please help me and suggest me to get android unique device ID.
Asked
Active
Viewed 435 times
1 Answers
-1
On a device first boot, a randomly value is generated and stored. This value is available via Settings.Secure.ANDROID_ID . It’s a 64-bit number that should remain constant for the lifetime of a device. ANDROID_ID seems a good choice for a unique device identifier because it’s available for smartphones and tablets. To retrieve the value, you can use the following
import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID);
//Secure ID V 10
val secureId = Secure.getString(getContentResolver(), Secure.ANDROID_ID)
//UUID
private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";public synchronized static String id(Context context) {
if (uniqueID == null) {
SharedPreferences sharedPrefs = context.getSharedPreferences(
PREF_UNIQUE_ID, Context.MODE_PRIVATE);
uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
if (uniqueID == null) {
uniqueID = UUID.randomUUID().toString();
Editor editor = sharedPrefs.edit();
editor.putString(PREF_UNIQUE_ID, uniqueID);
editor.commit();
}
}
return uniqueID;
}

Amir Gharajedaghi
- 11
- 6
-
Thanks for comment. Can you please tell me is this also works in android version > 10 ? – Anthony Shoshi Gomes Nov 28 '20 at 19:28
-
This isn't a valid "DEVICE unique ID" but only a "factory reset unique ID", in fact "Secure.ANDROID_ID" is reset after a factory reset (or manually using Apps which uses Root Permissions). Only using Root Permissions it could be possible to get some kind of DEVICE information that can be used as REAL UNIQUE data. – emandt Nov 28 '20 at 19:31
-
You can also try to get a MAC Address from a device having a Wi-Fi or Bluetooth hardware. But, this solution is not recommended because not all of the device have Wi-Fi connection. Even if the user have a Wi-Fi connection, it must be turned on to retrieve the data. Otherwise, the call doesn’t report the MAC Address. – Amir Gharajedaghi Nov 28 '20 at 19:51
-
As the requirement for most of applications is to identify a particular installation and not a physical device, a good solution to get unique id for an user if to use UUID class. The following solution has been presented by Reto Meier from Google in a Google I/O presentation – Amir Gharajedaghi Nov 28 '20 at 20:05
-
UUID.randomUUID() method generates an unique identifier for a specific installation. You have just to store that value and your user will be identified at the next launch of your application. You can also try to associate this solution with Android Backup service to keep the information available for the user even if he installs your application on an other device. – Amir Gharajedaghi Nov 28 '20 at 20:07
-
1Identify a particular device on Android is not an easy thing. There are many good reasons to avoid that. Best solution is probably to identify a particular installation by using UUID solution. However, if you want absolutely identify a particular device physically, you can try to use the ANDROID_ID solution. Not 100% reliable but better than other solutions. – Amir Gharajedaghi Nov 28 '20 at 20:16
-
I have stored UUID but whenever an user clear cache or storage, the UUID also removed from sharedprefs and then app cannot able to match the device by unique ID with the server stored unique ID. That's the problem I face. – Anthony Shoshi Gomes Nov 29 '20 at 18:32