A summary discussion of how to uniquely identify an Android device

Source: Internet
Author: User

Presumably when you're developing an Android project, you'll have a few more questions like "How to uniquely identify an Android device." Not only before, but even now and in the foreseeable future, this problem will persist.

If you use search tools to search, you can find a lot of solutions, but each one is somewhat flawed.

I'm here to explain the shortcomings of many common scenarios and recommend a relatively reliable approach.

First of all, explain the situation of Android devices. As we all know, at first the Android device simply means "mobile". It would be nice if that were the case. But that's not true, and we know that as Google and Android grow, Android is starting to grow in other areas, in addition to supporting mobile phones. As of Android 5.0, Android has been able to support handsets, tablets, watches, eyewear, televisions, boxes, and cars in detail, such as handheld devices and wearables. All of these conditions will affect the actual effectiveness of each solution.

Okay, let's go to one by one. Uncover the mysteries of the shortcomings of each solution.

android_id

ANDROID_ID exists inandroid.provider.Settings.Secure.ANDROID_ID.它可以通过下面的方式获取得到:

1
View Code

This is a 64-bit 16 binary string. It is created and stored when the device is first started. However, when the device is restored to factory settings or brush, it will not exist and can only be regenerated when it is restarted. This is one of its disadvantages. And then, before 2.2, this is not a reliable approach. Second, among the popular devices produced by major handheld device manufacturers, there is a noticeable bug that many devices have the same android_id. So, android_id is not a perfect solution.

DEVICEID

DEVICEID, as the name implies, "Device ID". is added to the phone by the mobile phone operator. It can be obtained in the following ways:

1 Telephonymanager tm = (Telephonymanager) Getbasecontext (). Getsystemservice (Context.telephony_service ); 2 String tmdevice = "" + Tm.getdeviceid ();
View Code

Its return value will be different depending on the operator and may return the IMEI or Meid or ESN of the phone. Because it is added by the mobile phone operator, there may be many problems:

Non-Mobile: This unique identifier does not exist for non-mobile devices such as tablets, smart watches, TVs and even cars that cannot be called.

Sustainability issues: On devices with DeviceID, this value may not be cleaned up after a brush or factory setup. If this is the case, we may consider them to be the same device.

Priority issue: Read_phone_state permissions are required to obtain DeviceID. If you don't use the phone in your app, it can be disturbing to use this permission.

Known bug issues: implementations of this approach have bugs on some machines and will return some spam, such as 0 or "*".

So, DeviceID is not a perfect solution to this problem.

Simserialnumber

Simserialnumber is also put in by mobile phone operators, and it is obtained in such a way:

1 Telephonymanager tm = (Telephonymanager) Getbasecontext (). Getsystemservice (Context.telephony_service ); 2 String tmserial = "" + Tm.getsimserialnumber ();
View Code

Its deviceid is especially the same.

SerialNumber

It was introduced after 2.3. Its value exists in the Android.os.Build.SERIAL.SerialNumber. However, for devices that do not support calls, a unique DeviceID is required first.

MAC address

Perhaps someone might have thought of acquiring a MAC address based on the device's WiFi or Bluetooth hardware to uniquely identify the device. But this is not a recommended way to do it. Because it is not a problem can be obtained, because it requires the device WiFi or Bluetooth open. So, what if the device doesn't turn on Bluetooth? What about 2G, 3G, or 4G if the device doesn't have WiFi turned on? So it's also an unreliable way.

Uuid

The above several ways to identify Android devices are not satisfactory. Is there a way to reliably and uniquely identify Androi hardware devices?

Eh, wait a minute, do you have to use the device's hardware information to identify the Android device? Is it rare to use non-hardware to identify a device?

Well, the answer is UUID. The UUID (Universal unique ID) is the "Universal unique ID" of the software community, which is the number generated on a single machine, which guarantees that all machines in the same time and space are unique. Typically, the platform provides the generated APIs. Based on standard calculations developed by the Open Software Foundation (OSF), Ethernet card addresses, nanosecond-seconds, chip ID codes, and many possible numbers are used

The UUID is composed of the following parts:
    1. The current date and time, the first part of the UUID is related to time, and if you generate a UUID after a few seconds, the first part is different and the rest is the same.
    2. Clock sequence.
    3. Globally unique IEEE Machine identification number, if there is a network card, from the network card MAC address obtained, no network card is obtained in other ways.
The only drawback to the UUID is that the resulting string will be longer. The most common use of UUID for this standard is the GUID of Microsoft (Globals Unique Identifiers). In ColdFusion, the UUID can be easily generated with the Createuuid () function in the form of: Xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx (8-4-4-16), where each x is 0-9 or a-f A hexadecimal number within the range. The standard UUID format is: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx (8-4-4-4-12), which can be converted from cflib download CreateGUID () UDF.

It is common and reasonable for a developer to track every installation of an application. to track an app's installation, you can use UUID as an identity and create it when the app first runs after installation. The following is a class called "intallation", with a static method ID (context context) that returns the app's UUID. You can write more data about the installation to the "istallation" file.

1  Public classInstallation {2Private StaticString SID =NULL;3Private Static FinalString installation = "Installation";4 5 Public synchronized StaticString ID (context context) {6 if(SID = =NULL) {  7File installation =NewFile (Context.getfilesdir (), installation);8Try {9if(!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 (); +     } -}
Installation.java

It is a software-generated UUID that uniquely identifies the installation of the software on that device to identify the device, rather than through hardware information. Of course, this is done by keeping the UUID in a local file, and if you erase the app data or delete the file, the app will regenerate a UUID, which means the device's unique identifier has changed, which is a new device.

The UUID is a random value, so what if it repeats? OK, this is a string of 32 hexadecimal digits, it produces the probability of duplicate value is 16 of the 32 of the party one, to calculate the probability of their own!!!

A summary discussion of how to uniquely identify an Android device

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.