[Android] 获取Android设备的唯一识别码|设备号|序号|UUID

 

如何获取一个能唯一标识每台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

posted @ 2011-01-11 18:09  木乃猫  阅读(81712)  评论(0编辑  收藏  举报