How to obtain a sequence number that uniquely identifies each Android device?
There are many answers to this question, but most of them are valid only in some cases.
Based on tests:
- all devices can return a
telephonymanager. getdeviceid ()
- A
telephonymanager. getsimserialnumber ()
- for all CDMA devices,
getsimserialnumber ()
Returns a null value!
- All devices with a Google account can return one
Android_id
- All CDMA Devices
Android_id
AndTelephonymanager. getdeviceid ()
Returns the same value (as long as a Google account is added at the time of setup)
-
- Not tested yet: GSM devices without SIM cards, GSM devices without Google accounts, and devices in flight mode.
So if you want to get the unique serial number of the device,Telephonymanager. getdeviceid ()
That's enough. However, obviously exposing DeviceID will make some users dissatisfied, so it is best to encrypt these IDs. In fact, the encrypted serial number can still uniquely identify the device, and does not explicitly expose the user's specific device, for example, using string. hashcode (), combined with UUID:
Final telephonymanager TM = (telephonymanager) getbasecontext (). getsystemservice (context. telephony_service); final string tmdevice, tmserial, tmphone, 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 uniqueid = deviceuuid. tostring ();
The final DeviceID may be as follows:201710000-54b3-e7c7-0000-000046bffd97