獲取Android裝置的唯一識別碼|裝置號|序號|UUID
阿新 • • 發佈:2018-12-30
如何獲取一個能唯一標識每臺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:
1 2 3 4 5 6 7 8 9 |
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