It is very simple to get the device's unique identity under Android (at least not to disable it like iOS, disable that), but there are also complex things to consider because of the diversity of devices.
Attach the complete code first
[Java]View Plaincopy
- Protected static final String prefs_file = "Gank_device_id.xml";
- protected static final String prefs_device_id = "gank_device_id";
- protected static String uuid;
- static public String Getudid ()
- {
- if (uuid = =null) {
- synchronized (gankmainactivity. Class) {
- if (uuid = = null) {
- final Sharedpreferences prefs = s_instance.getsharedpreferences (Prefs_file, 0);
- final String id = prefs.getstring (prefs_device_id, null);
- if (id! = null) {
- //Use the IDs previously computed and stored in the Prefs file
- UUID = ID;
- } Else {
- final String androidid = secure.getstring (S_instance.getcontentresolver (), secure.android_id);
- //Use the Android ID unless it's broken, in which case fallback on DeviceId,
- //Unless it ' s not available, then fallback on a random number which we store
- //To a prefs file
- try {
- if (!" 9774d56d682e549c ". Equals (Androidid)) {
- UUID = Uuid.nameuuidfrombytes (androidid.getbytes ("UTF8")). ToString ();
- } Else {
- final String deviceId = ((Telephonymanager) S_instance.getsystemservice (Context.telephony_service)). Getdeviceid ();
- UUID = deviceid!=null? Uuid.nameuuidfrombytes (deviceid.getbytes ("UTF8")). ToString (): Uuid.randomuuid (). ToString ();
- }
- } catch (Unsupportedencodingexception e) {
- throw New RuntimeException (e);
- }
- //Write The value out to the Prefs file
- Prefs.edit (). putstring (prefs_device_id, UUID). commit ();
- }
- }
- }
- }
- return UUID;
- }
1, under normal circumstances can pass ((Telephonymanager) S_instance.getsystemservice (Context.telephony_service)). Getdeviceid (); To get, but some tablets this function will return empty
2, through Secure.getstring (S_instance.getcontentresolver (), secure.android_id); You can also get to an ID, but android2.2 or some cottage phone use this also has a problem, it will return a fixed value 9774d56d682e549c
3, if the first two did not get to the Udid, then the program starts when the creation of a random uuid, and then saved up. This is a compatibility scheme, of course, this kind of equipment will not be many.
Get device Unique ID under Android (UDID, DeviceID ...)