• Log in or sign up for an account here: https://www.sythe.org/login

Is there a way to get a unique device ID in android?

Joined
Aug 21, 2009
Sythe Gold
0
Messages
14,450
Location
Canada
I want to be able to identify users without them registering for anything so I am creating a unique id for them if it is the first time they run the app. From what I looked online it doesn't look promising to get a truly unique device ID. I read some stuff about Froyo (2.2) botching the ANDROID_ID and some device manufactures screwing up where all ANDROID_ID are all the same. Well first off I don't even plan on letting the api go as low as 8 (I am going to set 10 minimum) so there goes that problem. I also read that in api 4.2+ where it allows profiles on a device, each profile is given a unique id. Is there anyway around this (assigning just 1 id to the whole device, not per profile)? This isn't really that big of a concern, although I would prefer not to allow it.

The main priority is NEVER letting two users share the same device id. This would be very bad for the app I am making. It would be a plus if the unique device id remains the same if they were to uninstall and redownload my app, however I am not so much worried about this as opposed to users sharing a unique device id. If the unique device id for their device changes when they reinstall the app after deleting it, so be it, as long as it doesn't match any other device id in my database. As for the situation in which someone does a factory reset on their phone, I would prefer the device id to stay the same, although if this can't be done no big deal either.

Here is what I have so far from what I found online:

Code:
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);

final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
String deviceId = deviceUuid.toString();


Does anyone see an issue with this? Once again, I am only targeting api 2.4 and above.
 
I actually wrote something like this before by creating a random 10 digit string. But you code is better since there cant be duplicates.


This is how you can make the code only run when the app is runs for the first time:
SharedPreferences settings = getSharedPreferences("MyPrefs", Context.MODE_WORLD_READABLE);
uniqueId=settings.getString("uniqueId","notSet");

if (uniqueId.equals("notSet")) {
...}

String uniqueId;
private void createUniqueId(){
SharedPreferences settings = getSharedPreferences("MyPrefs", Context.MODE_WORLD_READABLE);
uniqueId=settings.getString("uniqueId","notSet");

if (uniqueId.equals("notSet")) {

Toast.makeText(this, "Creating unique ID", Toast.LENGTH_LONG).show();
//Create unique ID at first run

uniqueId = RandomStringUtils.randomAlphanumeric(10).toUpperCase();//iwbn8
Log.v("unique",uniqueId);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("FirstTime", false);
editor.putString("uniqueId", uniqueId);
editor.commit();
}

TextView tv1 = (TextView)findViewById(R.id.id);
Log.v("private id", uniqueId);
tv1.setText(uniqueId);


}
 
duelarena.gg
Back
Top