Android Unique identification code

Source: Internet
Author: User

We will use the unique identification code of the device more or less in the course of the project, we hope to get a stable and reliable device unique identification code. Today we will cover several ways.

1. device_id assumes that we do need to use the identity of the real device and 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:
    • 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 MAC address as the device ID via WiFi or Bluetooth device on the phone, but it is not recommended because 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 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 first run of the program installation, but unlike the device's unique identity, it produces different IDs for different applications than the device unique IDs. 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:

public class Installation {
private static String SID = null;
Private static final String installation = "Installation";

Public synchronized static String ID (context context) {
if (SID = = null) {
File installation = new file (Context.getfilesdir (), installation);
try {
if (!installation.exists ())
Writeinstallationfile (installation);
SID = readinstallationfile (installation);
} catch (Exception e) {
throw new RuntimeException (e);
}
}
return SID;
}

private static String Readinstallationfile (File installation) throws IOException {
Randomaccessfile f = new Randomaccessfile (Installation, "R");
byte[] bytes = new byte[(int) f.length ()];
f.readfully (bytes);
F.close ();
return new String (bytes);
}

private static void Writeinstallationfile (File installation) throws IOException {
FileOutputStream out = new FileOutputStream (installation);
String id = uuid.randomuuid (). toString ();
Out.write (Id.getbytes ());
Out.close ();
}
}

Summarize

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:

Import Android.content.Context;
Import android.content.SharedPreferences;
Import Android.provider.Settings.Secure;
Import Android.telephony.TelephonyManager;

Import java.io.UnsupportedEncodingException;
Import Java.util.UUID;

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) {
Use the IDs previously computed and stored in the Prefs file
UUID = uuid.fromstring (ID);

} else {

Final String Androidid = secure.getstring (Context.getcontentresolver (), secure.android_id);

Use the Android ID unless it's broken, in which case fallback on deviceId,
Unless it ' s not available and then fallback on a random number which we store
to a prefs file
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);
}

Write the value out to the Prefs file
Prefs.edit (). putstring (prefs_device_id, uuid.tostring ()). commit ();

}

}
}
}

}


/**
* 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
* Usable value.
*
* 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.
*
* 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.
*
* Works around a bug in Android 2.2 for many devices when using android_id directly.
*
* @see http://code.google.com/p/android/issues/detail?id=10603
*
* @return A UUID that May is used to uniquely identify your device for most purposes.
*/
Public UUID Getdeviceuuid () {
return UUID;
}
}

How do I get a unique ID for my Android phone?

Code: Here's how you read the only Imsi-id/imei-id on Android.
Java:
String Myimsi = Android.os.SystemProperties.get (Android.telephony.TelephonyProperties.PROPERTY_IMSI);
Within my emulator it returns:310995000000000

String Myimei = Android.os.SystemProperties.get (Android.telephony.TelephonyProperties.PROPERTY_IMEI);
Within my emulator it returns:000000000000000
Note: The Android.os.SystemProperties label is @hide, so the SDK does not exist. If you need to use it, you need to have Android source code support.

Android Unique identification code

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.