Document directory
- 1. device_id
- 2. MAC address
- 3. Serial number
- 4. android_id
- 5. installtion ID: UUID
This article Reprinted from: http://blog.csdn.net/billpig/article/details/6728573
The unique identifier of the device is more or less used in the project. We hope to obtain a stable and reliable unique identifier of the device. 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. The device_id can be the same as that through telephonymanager. getdeviceid () is obtained. It returns the IMEI, meid, or ESN code based on different mobile phone devices. However, it may 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 phone's wifi or bluetooth device, but we do not recommend this because not all devices have WiFi, and if the 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, another solution is to use UUID, which does not need to access device resources, it is also irrelevant to 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. It generates different IDs for different applications, instead of the unique ID of the device. Therefore, it is often used to identify the unique ID (installtion ID) in an application, or to track the number of installed applications. Fortunately, Google developer blog provides a framework like this:
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(); }}
Summary
Based on the above, in order to obtain the unique identifier of a device more commonly on the device, we can implement such a class to generate a unique UUID for each device, which is based on android_id, in case of failed retrieval, use telephonymanager. getdeviceid () is an alternative method. If it fails again, use the uuid generation policy.
Reiterate that the following method is to generate the device ID. In most cases, the installtion ID can meet our needs. However, if you do need the device ID, you can implement it 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, 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 all UUIDs, this unique ID is "very highly likely" * to be unique across all Android devices. Much more so 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 be 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 may change. In particular, if the device is factory reset a new device ID * may be 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 may change. Or, if a user uninstalls your app on * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation. * * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will 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 be used to uniquely identify your device for most purposes. */ public UUID getDeviceUuid() { return uuid; }}
Reference:
Http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
Http://stackoverflow.com/questions/5088474/how-can-i-get-the-uuid-of-my-android-phone-in-an-application
All rights reserved. For more information, see the source!