"Android" Device ID

Source: Internet
Author: User

Android systems and devices have a lot of "identification" numbers, such as common imei,serizalnumber,uuid concepts, but there is some degree of unreliability, in the end how to mark an Android device?

The content of the article is more from:

1) http://www.cnblogs.com/lvcha/p/3721091.html

2) http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

In particular, the second article, is the source of many articles on the Internet, the first article is in 2 to be supplemented, collated this article.

1. device_id

Assuming we do need to use the identity of the real device, we may need to use device_id. In the past, our Android device is a mobile phone, this device_id can be obtained by Telephonymanager.getdeviceid (), it returns Imei,meid or ESN code according to different mobile devices, However, it will encounter many problems in the process of use:

1) Non-mobile device: If only the device with WiFi or music player does not have the hardware function of the call, there is no such device_id

2) Permissions: Get device_id need read_phone_state permission, but if we just to get it, no other call function, then this permission is a bit large to small use

3) BUG: On a few mobile devices, the implementation has a loophole, will return garbage, such as: zeros (all 0) or asterisks (asterisk) products

2. MAC ADDRESS

We can also get the MAC address as the device ID from the phone's WiFi or Bluetooth device, but this is not recommended because:

1) Hardware limitations: Not all devices have WiFi and Bluetooth hardware, the hardware does not exist naturally will not get this information.
2) Access limit: If WiFi is not turned on, it is unable to get its MAC address, and Bluetooth is only available when it is opened to its MAC address.

3. Serial number

The Android 2.3 can be obtained via Android.os.Build.SERIAL, and non-mobile devices can be obtained through this interface. Devices that do not have a telephony feature are required to provide such a serial number.

4. android_id

ANDROID_ID is a number of 64bit that are generated and stored at the first boot of the device, and the reset android_id appears to be a good choice for obtaining the device ID when the device is wipe, but it also has a flaw:

1) It is reliable and stable in Android <=2.1 or Android >=2.3 version, but in the 2.2 version it is not 100% reliable

2) in the mainstream manufacturers of equipment, there is a very frequent bug, that every device will produce the same android_id:9774d56d682e549c

3) Manufacturer Custom system bug: Some devices return a value of NULL.

4) Device differences: For CDMA devices, android_id and Telephonymanager.getdeviceid () return the same values.

5. Installtion Id:uuid

The above four ways have a certain limitation or bug, here, there is another way to solve, is to use the UUID, the method does not need to access the resources of the device, and is not related to the device type.

This is done by generating an ID after the program is installed for the first time ( Note: It is generated by itself, not by the system!) ), but this is not the same as the device's unique identity, it will produce different IDs for different applications than the device unique ID. This is often used to identify a unique ID in an app (that is, the Installtion ID), or to track the number of installs of an app. Fortunately, Google Developer blog provides such a framework:

1  Public classInstallation {2     Private StaticString SID =NULL;3     Private Static FinalString installation = "Installation";4 5      Public synchronized StaticString ID (context context) {6         if(SID = =NULL) {7File installation =NewFile (Context.getfilesdir (), installation);8             Try {9                 if(!installation.exists ())Ten writeinstallationfile (installation); OneSID =readinstallationfile (installation); A}Catch(Exception e) { -                 Throw NewRuntimeException (e); -             } the         } -         returnSID; -     } -  +     Private StaticString readinstallationfile (File installation)throwsIOException { -Randomaccessfile f =NewRandomaccessfile (Installation, "R"); +         byte[] bytes =New byte[(int) F.length ()]; A f.readfully (bytes); at f.close (); -         return NewString (bytes); -     } -  -     Private Static voidWriteinstallationfile (File installation)throwsIOException { -FileOutputStream out =NewFileOutputStream (installation); inString ID =Uuid.randomuuid (). toString (); - Out.write (Id.getbytes ()); to out.close (); +     } -}
View Code

In general, in order to achieve a more general acquisition device unique identity on the device, we can implement a class that produces a unique UUID for each device, based on android_id, with Telephonymanager.getdeviceid () when the acquisition fails For alternative methods, if you fail again, use the build strategy of the UUID.

Again, the following approach is to generate the device ID, in most cases the Installtion ID will meet our needs, but if you do need a device ID, it can be done in the following ways:

1 ImportAndroid.content.Context;2 Importandroid.content.SharedPreferences;3 Importandroid.provider.Settings.Secure;4 ImportAndroid.telephony.TelephonyManager;5 6 Importjava.io.UnsupportedEncodingException;7 ImportJava.util.UUID;8 9  Public classDeviceuuidfactory {Ten     protected Static FinalString prefs_file = "Device_id.xml"; One     protected Static FinalString prefs_device_id = "device_id"; A  -     protected Staticuuid uuid; -  the     /** - * Returns A unique UUID for the current Android device. As with any UUIDs, this unique ID is "very highly likely" - * To is unique across all Android devices. Much than android_id is. -     * + * The UUID is generated by using android_id as the base key if appropriate, falling back on - * Telephonymanager.getdeviceid () If android_id is known to being incorrect, and finally falling back + * on a random UUID that's persisted to sharedpreferences if Getdeviceid () does not return a A * Usable value. at     * - * In some rare circumstances, this ID could change. In particular, if the device is factory reset a new device ID - * May is generated. In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2 - * to a newer, non-buggy version of Android, the device ID is change. Or, if a user uninstalls your app on - * A device that has neither a proper an Android ID nor a device ID, this ID may change on reinstallation. -     * in * Note that if the code falls is on using Telephonymanager.getdeviceid (), the resulting ID would not - * Change after a factory reset. Something to be aware of. to     * + * Works around a bug in Android 2.2 for many devices when using android_id directly. -     * the     * @see http://code.google.com/p/android/issues/detail?id=10603 *     * $     * @returna UUID that is used to uniquely identify your device for most purposes.Panax Notoginseng     */ -      Publicdeviceuuidfactory (Context context) { the         if(UUID = =NULL ) { +             synchronized(Deviceuuidfactory.class) { A                 if(UUID = =NULL) { the                     FinalSharedpreferences prefs = context.getsharedpreferences (prefs_file, 0); +                     FinalString id = prefs.getstring (prefs_device_id,NULL ); -  $                     if(id! =NULL) { $                         //Use the IDs previously computed and stored in the Prefs file -UUID =ID; -}Else { the                         FinalString Androidid =secure.getstring (Context.getcontentresolver (), secure.android_id); - Wuyi                         //Use the Android ID unless it's broken, in which case fallback on DeviceId, the                         //unless it ' s not available and then fallback on a random number which we store -                         //To a prefs file Wu                         Try { -                             if(!" 9774d56d682e549c ". Equals (Androidid)) { AboutUUID = Uuid.nameuuidfrombytes (androidid.getbytes ("UTF8")); $}Else { -                                 FinalString deviceId =((Telephonymanager) Context.getsystemservice (Context.telephony_service)). Getdeviceid (); -UUID = deviceId! =NULL? Uuid.nameuuidfrombytes (Deviceid.getbytes ("UTF8"): Uuid.randomuuid (); -                             } A}Catch(unsupportedencodingexception e) { +                             Throw NewRuntimeException (e); the                         } -  $                         //Write the value out to the prefs file the Prefs.edit (). putstring (prefs_device_id, uuid.tostring ()). commit (); the  the                     } the                 } -             } in         } the     } the}
View Code

The device flag number generated in this way can be used to mark an Android device to a certain degree more stably. Here are a few points to note about this method of generating markers:

1) Since Android phones are now generally upgraded to more than 4.0, 2.3 support is limited, so android_id generally available, that is, the android_id as the seed generation UUID is basically available;

2) using DeviceID for seed generation UUID is not a good choice, Android tablet and other devices do not have the communication function can not get DeviceID;

3) There is a requirement that you want the user's device to be able to uniquely determine the device, no matter how many times it is installed, what is the available way? We have to consider both Android_id,serizal number and device_id, either of which can identify the device ~~so, and the best choice is whether to combine the three as a UUID seed? Among them, we can rule out the illegal device_id and so on;

Check for illegality:

1) The serial number itself is repeated: for example, all 000000000;

2) is null and is used to not get the relevant data;

3) Whether the * number is included;

If the three points are satisfied at any point, it means that the data is illegal, can be discarded, otherwise, the character stitching as the seed of the UUID, if three kinds of data are illegal, can be randomly generated UUID as the device identifier.

"Android" Device ID

Related Article

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.