zoukankan      html  css  js  c++  java
  • 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:
    1. final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
    2. final String tmDevice, tmSerial, tmPhone, androidId;
    3. tmDevice = "" + tm.getDeviceId();
    4. tmSerial = "" + tm.getSimSerialNumber();
    5. androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    6. UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
    7. String uniqueId = deviceUuid.toString();
    复制代码
    最后的deviceID可能是这样的结果: 00000000-54b3-e7c7-0000-000046bffd97
  • 相关阅读:
    python-44-初识队列
    python-43-进程锁/信号量/事件
    python-42-Process多进程
    python-41-初识hmac与socketserver模块
    python-40-初识socket与struct
    python-39-hashlib与logging模块
    python-38-用于面向对象的内置函数
    python-37-各种反射
    python-36-封装与面向对象函数
    python-35-多态与初识封装
  • 原文地址:https://www.cnblogs.com/xin36933/p/3554371.html
Copyright © 2011-2022 走看看