Android 獲取惟一機器碼的代碼

作應用時不少時候都得獲取到每一個設備的機器碼android

  1. 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

  2. final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
    ui

  3. final String tmDevice, tmSerial, tmPhone, androidId;
    加密

  4. tmDevice = "" + tm.getDeviceId();
    spa

  5. tmSerial = "" + tm.getSimSerialNumber();
    orm

  6. androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    get

  7. UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
    hash

  8. String uniqueId = deviceUuid.toString();
    import

  9. 最後的deviceID多是這樣的結果: 00000000-54b3-e7c7-0000-000046bffd97im

相關文章
相關標籤/搜索