標籤:
Android系統以及裝置都有很多的“標識”號,比如常見的IMEI,SerizalNumber,UUID等概念,但是這些都存在一定程度上的不可靠性,到底如何標記一台Android裝置?
文章內容多來自:
1)http://www.cnblogs.com/lvcha/p/3721091.html
2)http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
尤其第二篇,乃是網上許多文章的根源所在,第一篇文章則是在2)上得補充,整理得本文。
1. DEVICE_ID
假設我們確實需要用到真實裝置的標識,可能就需要用到DEVICE_ID。在以前,我們的Android裝置是手機,這個DEVICE_ID可以同通過TelephonyManager.getDeviceId()擷取,它根據不同的手機裝置返回IMEI,MEID或者ESN碼,但它在使用的過程中會遇到很多問題:
1)非手機裝置: 如果只帶有Wifi的裝置或者音樂播放器沒有通話的硬體功能的話就沒有這個DEVICE_ID
2)許可權: 擷取DEVICE_ID需要READ_PHONE_STATE許可權,但如果我們只為了擷取它,沒有用到其他的通話功能,那這個許可權有點大才小用
3)bug:在少數的一些手機裝置上,該實現有漏洞,會返回垃圾,如:zeros(全是0)或者asterisks(星號)的產品
2. MAC ADDRESS
我們也可以通過手機的Wifi或者藍牙裝置擷取MAC ADDRESS作為DEVICE ID,但是並不建議這麼做,因為:
1)硬體限制:並不是所有的裝置都有Wifi和藍芽硬體,硬體不存在自然也就得不到這一資訊。
2)擷取的限制:如果Wifi沒有開啟過,是無法擷取其Mac地址的;而藍芽是只有在開啟的時候才能擷取到其Mac地址。
3. Serial Number
在Android 2.3可以通過android.os.Build.SERIAL擷取,非手機裝置可以通過該介面擷取。沒有電話功能的裝置被要求必須提供這樣一個序號。
4. ANDROID_ID
ANDROID_ID是裝置第一次啟動時產生和儲存的64bit的一個數,當裝置被wipe後該數重設ANDROID_ID似乎是擷取Device ID的一個好選擇,但它也有缺陷:
1)它在Android <=2.1 or Android >=2.3的版本是可靠、穩定的,但在2.2的版本並不是100%可靠的
2)在主流廠商生產的裝置上,有一個很經常的bug,就是每個裝置都會產生相同的ANDROID_ID:9774d56d682e549c
3)廠商定製系統的Bug:有些裝置返回的值為null。
4)裝置差異:對於CDMA裝置,ANDROID_ID和TelephonyManager.getDeviceId() 返回相同的值。
5. Installtion ID : UUID
以上四種方式都有或多或少存在的一定的局限性或者bug,在這裡,有另外一種方式解決,就是使用UUID,該方法無需訪問裝置的資源,也跟裝置類型無關。
這種方式是通過在程式安裝後第一次運行後產生一個ID實現的(注意:是自己產生,而非系統主動產生的!),但該方式跟裝置唯一標識不一樣,它會因為不同的應用程式而產生不同的ID,而不是裝置唯一ID。因此經常用來標識在某個應用中的唯一ID(即Installtion ID),或者跟蹤應用的安裝數量。很幸運的,Google Developer Blog提供了這樣的一個架構:
1 public class Installation { 2 private static String sID = null; 3 private static final String INSTALLATION = "INSTALLATION"; 4 5 public synchronized static String id(Context context) { 6 if (sID == null) { 7 File installation = new File(context.getFilesDir(), INSTALLATION); 8 try { 9 if (!installation.exists())10 writeInstallationFile(installation);11 sID = readInstallationFile(installation);12 } catch (Exception e) {13 throw new RuntimeException(e);14 }15 }16 return sID;17 }18 19 private static String readInstallationFile(File installation) throws IOException {20 RandomAccessFile f = new RandomAccessFile(installation, "r");21 byte[] bytes = new byte[(int) f.length()];22 f.readFully(bytes);23 f.close();24 return new String(bytes);25 }26 27 private static void writeInstallationFile(File installation) throws IOException {28 FileOutputStream out = new FileOutputStream(installation);29 String id = UUID.randomUUID().toString();30 out.write(id.getBytes());31 out.close();32 }33 }View Code
綜合以上所述,為了實現在裝置上更通用的擷取裝置唯一標識,我們可以實現這樣的一個類,為每個裝置產生唯一的UUID,以ANDROID_ID為基礎,在擷取失敗時以TelephonyManager.getDeviceId()為備選方法,如果再失敗,使用UUID的建置原則。
重申下,以下方法是產生Device ID,在大多數情況下Installtion ID能夠滿足我們的需求,但是如果確實需要用到Device ID,那可以通過以下方式實現:
1 import android.content.Context; 2 import android.content.SharedPreferences; 3 import android.provider.Settings.Secure; 4 import android.telephony.TelephonyManager; 5 6 import java.io.UnsupportedEncodingException; 7 import java.util.UUID; 8 9 public class DeviceUuidFactory {10 protected static final String PREFS_FILE = "device_id.xml";11 protected static final String PREFS_DEVICE_ID = "device_id";12 13 protected static UUID uuid;14 15 /**16 * Returns a unique UUID for the current android device. As with all UUIDs, this unique ID is "very highly likely"17 * to be unique across all Android devices. Much more so than ANDROID_ID is.18 *19 * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on20 * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back21 * on a random UUID that‘s persisted to SharedPreferences if getDeviceID() does not return a22 * usable value.23 *24 * In some rare circumstances, this ID may change. In particular, if the device is factory reset a new device ID25 * may be generated. In addition, if a user upgrades their phone from certain buggy implementations of Android 2.226 * to a newer, non-buggy version of Android, the device ID may change. Or, if a user uninstalls your app on27 * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation.28 *29 * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT30 * change after a factory reset. Something to be aware of.31 *32 * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly.33 *34 * @see http://code.google.com/p/android/issues/detail?id=1060335 *36 * @return a UUID that may be used to uniquely identify your device for most purposes.37 */38 public DeviceUuidFactory(Context context) {39 if( uuid ==null ) {40 synchronized (DeviceUuidFactory.class) {41 if( uuid == null) {42 final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);43 final String id = prefs.getString(PREFS_DEVICE_ID, null );44 45 if (id != null) {46 // Use the ids previously computed and stored in the prefs file47 uuid = id;48 } else {49 final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);50 51 // Use the Android ID unless it‘s broken, in which case fallback on deviceId,52 // unless it‘s not available, then fallback on a random number which we store53 // to a prefs file54 try {55 if (!"9774d56d682e549c".equals(androidId)) {56 uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));57 } else {58 final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();59 uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();60 }61 } catch (UnsupportedEncodingException e) {62 throw new RuntimeException(e);63 }64 65 // Write the value out to the prefs file66 prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).commit();67 68 }69 }70 }71 }72 }73 }View Code
通過這種方式產生的裝置標誌號在一定程度上可以比較穩定的標記一台Android裝置。這個產生標記符的方法有以下幾點要注意:
1)由於現在Android手機已經普遍升級到4.0以上,2.3以下支援有限,所以ANDROID_ID一般可用,即以ANDROID_ID為種子產生UUID基本可用;
2)使用DeviceID為種子產生UUID並非好的選擇,Android平板等不具備通訊功能的裝置就不能拿到DeviceID;
3)加入有一個需求是希望使用者的裝置無論在安裝多少次應用之後都可以唯一的確定該裝置,則可用的方式是什麼呢?我們得同時考慮ANDROID_ID,Serizal Number以及DEVICE_ID,其中任何一個都可以標識裝置~~SO,最好的選擇是否是將這三個拼接起來作為UUID的種子呢?其中,我們可以排除掉不合法的DEVICE_ID等;
檢查非法性:
1)序號本身字元是否重複:比如全是000000000;
2)是否為Null,用於無法拿到相關資料;
3)是否包含*號;
如果這三點中得任意一點滿足,則表示該資料非法,可以丟棄使用,否則,可字元拼接作為UUID的種子,如果三種資料都非法,才可以隨機產生UUID作為裝置標識符。
【Android】裝置標識