Learn how to get the unique identifier of an android device (21)

Source: Internet
Author: User

Learn how to get the unique identifier of an android device (21)

Because a stable and reliable unique device identification code is required for the project, some information on the internet is searched. We will introduce several methods today.

1. DEVICE_ID

If we do need the identifier of a real device, DEVICE_ID may be used. In the past, our Android device was a mobile phone.


DEVICE_ID can be obtained through TelephonyManager. getDeviceId (). It returns IMEI, MEID, or ESN based on different mobile devices.


But it will encounter many problems during use:


  • Non-mobile device: This DEVICE_ID is not available if only Wifi devices or music players do not have the hardware function for communication.
  • Permission: The READ_PHONE_STATE permission is required to obtain the DEVICE_ID. However, if we only want to obtain the DEVICE_ID and do not use other call functions, this permission is a little small.
  • Bug: In a few mobile devices, this vulnerability will return garbage, such as zeros or asterisks products.

 

2. MAC ADDRESS

We can also obtain the mac address as the device id through the Wifi or bluetooth DEVICE of the mobile phone, but we do not recommend this because it is not


All devices have Wifi, And if Wifi is not enabled, the hardware device cannot return mac address.

 

3. Serial Number

In Android 2.3, you can use android. OS. Build. SERIAL to obtain data. Non-mobile devices can use this interface.


4. ANDROID_ID

ANDROID_ID is the number of 64bits generated and stored when the device is started for the first time. This number is reset when the device is set to wipe.


ANDROID_ID seems to be a good choice for obtaining the Device ID, but it also has defects:

 

  • It is reliable and stable in Android <= 2.1 or Android> = 2.3, but it is not reliable in Android 2.2.
  • A common bug occurs on devices produced by mainstream manufacturers, that is, each device generates the same ANDROID_ID: 9774d56d682e549c.

 

5. Installtion ID: UUID

The above four methods have some limitations or bugs. Here, there is another solution, that is, using UUID, which


You do not need to access the device resources or the device type.


This method is implemented by generating an ID after the first operation after the program is installed. However, this method is different from the unique identifier of the device.


Different IDs are generated by the application, rather than the unique ID of the device. Therefore, it is often used to identify the unique ID (Installtion ID) in an application, or


Users track the number of applications installed. Fortunately, Google Developer Blog provides a framework like this:


Let's look at the following program: (generate a unique UUID for each device, based on ANDROID_ID, and use TelephonyManager when the acquisition fails. getDeviceId () is an alternative method. If it fails again, use the UUID generation policy .)


public class DeviceUuidFactory {protected static final String PREFS_FILE = "device_id.xml";protected static final String PREFS_DEVICE_ID = "device_id";protected static UUID uuid;public DeviceUuidFactory(Context context) {if (uuid == null) {synchronized (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();}}}}}}



Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44194763 sentiment control _

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.