轉載自:http://www.cnblogs.com/xiaowenji/archive/2011/01/11/1933087.html
如何獲取一個能唯一標識每臺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());
String uniqueId = deviceUuid.toString();
最後的deviceID可能是這樣的結果: 00000000-54b3-e7c7-0000-000046bffd97