I saw one of my best question Is there a unique Android device ID?
I need a solution for Android 10 too.
I used some following code to get Unique Id.
public static String getDeviceId(Activity context) {
PermissionsChecker checker = new PermissionsChecker(context);
if (checker.lacksPermissions(Manifest.permission.READ_PHONE_STATE))
PermissionsActivity.startActivityForResult(context, 0, REQUIRED_PERMISSION);
else {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice = tm.getDeviceId();
final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32));
return deviceUuid.toString();
}
return null;
}
But I am getting some warning on hover on
tm.getDeviceId();
and
Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
as following:
Using getString to get device identifiers is not recommended.
Using device identifiers is not recommended other than for high value fraud prevention and advanced telephony use-cases. For advertising use-cases, use
AdvertisingIdClient$Info#getId
and for analytics, useInstanceId#getId.
Is there any solution? Is it harmful or anything else?
The very easy answer is if you need to uniquily identify each user of your app create a random id at first use.
UUID.randomUUID().toString()
You can save it in SharedPreferences
and the easy work is done. No permissions needed
The problem is that it wont help you if user unninstall and reinstall the app.
One easy trick that is possible to do until android 10 (and wont be possible anymore on android 11 according to google) is save it in a hidden file in the external storage...
then on first use of app you look for such file, if it exists read the UUID, case not generate a new one and save the file.
The problem with this strategy is YOU NEED WRITE_EXTERNAL_STORAGE
PERMISSION
in case this isnt a problem simple do...
new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + ".my_hidden_folder").mkdir();
Any file saved inside this folder will be hidden to user, wont be removed on unninstall of your app
I personally use
Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
This isn't harmful is just that google dont like you doing it