Android獲取裝置唯一ID的幾種方式
先來看看幾種比較單一的方式:
IMEI
方式:TelephonyManager.getDeviceId():
問題
範圍:只能支援擁有通話功能的裝置,對於平板不可以。
永續性:返廠,資料擦除的時候不徹底,保留了原來的標識。
許可權:需要許可權:android.permission.READ_PHONE_STATE
bug: 有些廠家的實現有bug,返回一些不可用的資料
Mac地址
ACCESS_WIFI_STATE許可權
有些裝置沒有WiFi,或者藍芽,就不可以,如果WiFi沒有開啟,硬體也不會返回Mac地址,不建議使用
ANDROID_ID
2.2(Froyo,8)版本系統會不可信,來自主要生產廠商的主流手機,至少有一個普遍發現的bug,這些有問題的手機相同的ANDROID_ID: 9774d56d682e549c
但是如果返廠的手機,或者被root的手機,可能會變
Serial Number
從Android 2.3 (“Gingerbread”)開始可用,可以通過android.os.Build.SERIAL獲取,對於沒有通話功能的裝置,它會
返回一個唯一的device ID,
以下幾個是stackoverflow上評論較多的幾個,沒貼完,還有其他,綜合的,用到以上的部分方式:
有興趣的朋友可以再仔細看看
支援率比較高的(支援票數157):androidID --> 剔除2.2版本(API 8)中有問題的手機,使用UUID替代
- 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;
- publicclass DeviceUuidFactory {
- protectedstaticfinal String PREFS_FILE = "device_id.xml";
- protectedstaticfinal String PREFS_DEVICE_ID = "device_id";
- protectedstaticvolatile 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) {
- thrownew 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;
- }
- }
根據版本進行判斷的方式:Serial序列號-->UUID (支援數31)
通過Serial 即可,在覆蓋率上,你已經成功的獲得了98.4%的使用者,剩下的1.6%的使用者系統是在9 以下的。
通過AndroidID獲取,前面已經說過,在8上,有些商家的手機會有一些bug,返回相同的AndroidID,如果Serial和AndroidID都不行
- /**
- * Return pseudo unique ID
- * @return ID
- */
- publicstatic String getUniquePsuedoID()
- {
- // If all else fails, if the user does have lower than API 9 (lower
- // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID'
- // returns 'null', then simply the ID returned will be solely based
- // off their Android device information. This is where the collisions
- // can happen.
- // Thanks http://www.pocketmagic.net/?p=1662!
- // Try not to use DISPLAY, HOST or ID - these items could change.
- // If there are collisions, there will be overlapping data