Learn about Android together how to obtain unique identification code notes for Android devices (21)

Source: Internet
Author: User

Because of the need to obtain a stable, reliable device unique identification code in the project, so search some online information . Today we will cover several ways.

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 was a mobile phone, this


DEVICE_ID can be obtained with Telephonymanager.getdeviceid (), which returns Imei,meid or ESN depending on the mobile device


The code, but it will encounter many problems in the course of use:


    • Non-mobile devices: If only the device with WiFi or the music player does not have the hardware function of the call, there is no such device_id
    • 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
    • BUG: On a small number of mobile devices, the implementation has a loophole, will return garbage, such as: zeros or asterisks 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 it is not


All devices have WiFi, and if WiFi is not turned on, the hardware device cannot return to 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.


4. android_id

ANDROID_ID is a number of 64bit that is generated and stored when the device is first started and resets when the device is wipe


ANDROID_ID seems to be a good choice to get a device ID, but it's also flawed:

    • It is reliable and stable in Android <=2.1 or Android >=2.3 version, but the version in 2.2 is not 100% reliable
    • In the mainstream manufacturers of equipment, there is a very frequent bug, that every device will produce the same android_id:9774d56d682e549c

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 party


Does not require access to the device's resources, and is not related to the device type.


This is done by generating an ID after the first run of the program after installation, but it is not the same as the device's unique identity, because it is different


The application produces a different ID than the device unique ID. Therefore, it is often used to identify a unique ID (that is, the Installtion ID) in an app, or


Installs the number of apps that are tracked by the user. Fortunately, Google Developer blog provides such a framework:


Let's take a look at the following program: ( generates a unique UUID for each device, based on android_id, with Telephonymanager.getdeviceid () as an alternative to getting the failure, and, if it fails again, the generation strategy of the UUID. )


public class Deviceuuidfactory {protected static final string prefs_file = "Device_id.xml";p rotected static final string P refs_device_id = "device_id";p rotected static UUID Uuid;public deviceuuidfactory (context context) {if (UUID = = null) {Sync Hronized (Deviceuuidfactory.class) {if (uuid = = null) {final Sharedpreferences prefs = context.getsharedpreferences (  Prefs_file, 0); final String id = prefs.getstring (prefs_device_id, NULL), if (id! = NULL) {UUID = uuid.fromstring (ID);} else {final String Androidid = secure.getstring (Context.getcontentresolver (), secure.android_id); try {if (!) 9774d56d682e549c ". Equals (Androidid)) {uuid = Uuid.nameuuidfrombytes (androidid.getbytes (" UTF8 "));} else {final String DeviceId = ((Telephonymanager) Context.getsystemservice (Context.telephony_service)). Getdeviceid (); uuid = deviceId! = Null? Uuid.nameuuidfrombytes (Deviceid.getbytes ("UTF8")): Uuid.randomuuid ();}} catch (Unsupportedencodingexception e) {throw new RuntimeException (e);} Prefs.edit (). putstring (Prefs_device_id, Uuid.tostring ()). commit ();}}}}} 



Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/44194763 Emotional Control _

Learn about Android together how to obtain unique identification code notes for Android devices (21)

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.