如何獲取一個能惟一標識每臺Android設備的序號?
這個問題有不少答案,可是他們中的大部分只在某些狀況下有效。
根據測試:
全部的設備均可以返回一個 TelephonyManager.getDeviceId()
全部的GSM設備 (測試設備都裝載有SIM卡) 能夠返回一個TelephonyManager.getSimSerialNumber()
全部的CDMA 設備對於 getSimSerialNumber() 卻返回一個空值!
全部添加有谷歌帳戶的設備能夠返回一個 ANDROID_ID
全部的CDMA設備對於 ANDROID_ID 和 TelephonyManager.getDeviceId() 返回相同的值(只要在設置時添加了谷歌帳戶)
目前還沒有測試的:沒有SIM卡的GSM設備、沒有添加谷歌帳戶的GSM設備、處於飛行模式的設備。
因此若是你想獲得設備的惟一序號, TelephonyManager.getDeviceId() 就足夠了。但很明顯暴露了DeviceID會使一些用戶不滿,因此最好把這些id加密了。實際上加密後的序號仍然能夠惟一的識別該設備,而且不會明顯的暴露用戶的特定設備,例如,使用 String.hashCode() ,結合UUID:
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, tmPhone, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());android
String uniqueId = deviceUuid.toString();ide
最後的deviceID多是這樣的結果: 00000000-54b3-e7c7-0000-000046bffd97測試