Get device Unique ID under Android (UDID, DeviceID ...)

Source: Internet
Author: User

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
  1. Protected static final String prefs_file = "Gank_device_id.xml";
  2. protected static final String prefs_device_id = "gank_device_id";
  3. protected static String uuid;
  4. static public String Getudid ()
  5. {
  6. if (uuid = =null) {
  7. synchronized (gankmainactivity. Class) {
  8. if (uuid = = null) {
  9. final Sharedpreferences prefs = s_instance.getsharedpreferences (Prefs_file, 0);
  10. final String id = prefs.getstring (prefs_device_id, null);
  11. if (id! = null) {
  12. //Use the IDs previously computed and stored in the Prefs file
  13. UUID = ID;
  14. } Else {
  15. final String androidid = secure.getstring (S_instance.getcontentresolver (), secure.android_id);
  16. //Use the Android ID unless it's broken, in which case fallback on DeviceId,
  17. //Unless it ' s not available, then fallback on a random number which we store
  18. //To a prefs file
  19. try {
  20. if (!" 9774d56d682e549c ". Equals (Androidid)) {
  21. UUID = Uuid.nameuuidfrombytes (androidid.getbytes ("UTF8")). ToString ();
  22. } Else {
  23. final String deviceId = ((Telephonymanager) S_instance.getsystemservice (Context.telephony_service)). Getdeviceid    ();
  24. UUID = deviceid!=null?    Uuid.nameuuidfrombytes (deviceid.getbytes ("UTF8")). ToString (): Uuid.randomuuid (). ToString ();
  25. }
  26. } catch (Unsupportedencodingexception e) {
  27. throw New RuntimeException (e);
  28. }
  29. //Write The value out to the Prefs file
  30. Prefs.edit (). putstring (prefs_device_id, UUID). commit ();
  31. }
  32. }
  33. }
  34. }
  35. return UUID;
  36. }



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 ...)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.