A while ago I was looking for a way to uniquely identify a device my application wa开发者_开发技巧s running on. I found the following algorithm:
final TelephonyManager tm = (TelephonyManager) baseContext.getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice = "" + tm.getDeviceId();
final String tmSerial = "" + tm.getSimSerialNumber();
final String androidId = "" + android.provider.Settings.Secure.getString(baseContext.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
return deviceUuid.toString();
Can anyone explain to me why this would occasionally be giving me differing results on the same device, and what I can do to correct it?
I expect the problem relates to getSimSerialNumber() as that is getting the serial number for one specific SIM card, so if the owner of the phone changes the SIM (e.g. by moving network) the method will return a different ID. The SIM is unique, but each device may use different SIMs at different times.
Advantej and Ollie C both posted the answer to this question. Have a look at the following page:
http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
精彩评论