作應用時不少時候都得獲取到每一個設備的機器碼android
Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID)
或者
全部的設備均可以返回一個TelephonyManager.getDeviceId()
全部的GSM設備能夠返回一個TelephonyManager.getSimSerialNumber()
全部的CDMA 設備對於 getSimSerialNumber() 卻返回一個空值!
全部添加有谷歌帳戶的設備能夠返回一個 ANDROID_ID
全部的CDMA設備對於 ANDROID_ID 和 TelephonyManager.getDeviceId() 返回相同的值(只要在設置時添加了谷歌帳戶)
正常狀況下,你想獲得設備的惟一序號, TelephonyManager.getDeviceId() 就足夠了。
但會暴露DeviceID,最好把這些id加密。加密後的序號仍然能夠惟一的識別該設備,
例如,使用 String.hashCode() ,結合UUID:ide
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
ui
final String tmDevice, tmSerial, tmPhone, androidId;
加密
tmDevice = "" + tm.getDeviceId();
spa
tmSerial = "" + tm.getSimSerialNumber();
orm
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
get
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
hash
String uniqueId = deviceUuid.toString();
import
最後的deviceID多是這樣的結果: 00000000-54b3-e7c7-0000-000046bffd97im