zoukankan      html  css  js  c++  java
  • Android能够获取到唯一的设备ID吗?

    Android是否有唯一的设备ID,如果有的话,该怎样快速有效获取?

    Settings.Secure#ANDROID_ID 返回Android ID ,是一个64位的16进制字符串

    1
    2
    3
    import android.provider.Settings.Secure;
     
    private String android_id = Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID);

    关于这个问题有很多的答案,不过其中大部分将只能算是答对了一部分,还不够好。

    根据我在很多设备上的测试(所有的电话,至少有一个是无效的)

    1. 所有设备针对 TelephonyManager.getDeviceId() 测试都有返回值
    2. 所有GSM设备(都有SIM卡)针对 TelephonyManager.getSimSerialNumber() 测试都有返回值
    3. 所有CDMA设备针对 getSimSerialNumber() 测试返回 NULL(预期中的)
    4. 所有有谷歌帐户的设备都返回了 ANDROID_ID 值
    5. 所有的CDMA设备针对 ANDROID_ID 和 TelephonyManager.getDeviceId() 都返回相同的值(或派生自同一个值) - 前提是安装过程中已经添加了谷歌帐户。
    6. 我还没有机会测试没有SIM卡的GSM设备,没有谷歌账户的GSM设备,以及任何在飞行模式下的设备。
    所以,如果你想要得到设备本身的唯一值, TM.getDeviceId()应该是足够了。不过显然的,有些用户比其他人更加偏执一些,可以将这些标识中的一个或者多个进行hash运算就很有用,生成的字符串仍然可以唯一标识该设备,但是不会明确的标识实际的设备。例如,使用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, 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 deviceId = deviceUuid.toString();

     

    结果可能类似 :00000000-54b3-e7c7-0000-000046bffd97

    对我来说这种方式已经足够了

    别忘了增加权限,用于读取 TelephonyManager properties,在manifest 中增加下面一行:

    1
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

     


  • 相关阅读:
    多线程案例
    Fault-Tolerance, Fast and Slow: Exploiting Failure Asynchrony in Distributed Systems
    LRU缓存实现案例
    kubernetes:基于ab的压力测试
    《软件测试52讲》——测试数据准备篇
    《软件测试52讲》——性能测试篇
    《软件测试52讲》——代码测试篇
    《软件测试52讲》——API自动化测试篇
    《软件测试52讲》——GUI自动化测试篇
    《梁宁产品思维30讲》——创新模式:找到创新模式,发现新大陆
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6469633.html
Copyright © 2011-2022 走看看