Android设备ID简析

来源:互联网 发布:圣火名尊法器进阶数据 编辑:程序博客网 时间:2024/06/07 05:31
   对于获取设备唯一ID一直以来由于生产商、Android版本、设备类型等不一致,导致根据设备开放的接口返回垃圾数据或为空值。目前Google官方给出的说法也是不完美的,暂时没有一个很完美的方法能够解决这个问题。   在此我们需要知道Android的几个设备概念:   Android四种唯一编号IMEI、MEID、ESN、IMSI,当Android设备作为电话使用时寻找唯一标识号比较简单可以根据以下方法获取。注:在手机不具备通话功能时,获取的设备ID可能为空,也有些厂家的实现有bug可能返回为无用值。
    /**     * getDeviceId() function Returns the unique device ID.     * for example,the IMEI for GSM and the MEID or ESN for CDMA phones.     *     * @param context     * @return     */    public static String getDeviceId(Context context) {        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);        return telephonyManager.getDeviceId();    }     /**     * getSubscriberId() function Returns the unique subscriber ID,     * for example, the IMSI for a GSM phone.     *     * @param context     * @return     */    public static String getSubscriberId(Context context) {        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);        return telephonyManager.getSubscriberId();    }    如要只读取手机的状态,则需添加READ_PHONE_STATE权限到AndroidManifest.xml文件中。
    Mac地址分为WiFiMac地址和蓝牙Mac地址,可以作为设备唯一值。但是也有一定的局限性:    1.当设备没有WiFi或蓝牙功能时,自然就会获取不到。    2.当WiFi没有打开过无法获取其Mac地址。    3.当蓝牙没有开启时也无法获取其Mac地址。    获取Mac地址的方法:
    /**     * getMacAddress() function Returns the unique MacAddress,     * @param context     * @return     */    public static String getMacAddress(Context context) {        WifiManager wifiManager = (WifiManager) context                .getSystemService(Context.WIFI_SERVICE);        WifiInfo wifiInfo = wifiManager.getConnectionInfo();        return wifiInfo.getMacAddress();    }
    ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,当设备被wipe后该数重置。ANDROID_ID有个缺陷是在主流厂商生产的设备上,有一个很经常的bug,就是每个设备都会产生相同的ANDROID_ID:9774d56d682e549c 。在刷机或者重置时ANDROID_ID的值都会变化。    获取ANDROID_ID的方法:
    /**     * getAndroidId() function Returns the unique ANDROID_ID,     *     * @param context     * @return     */    public static String getAndroidId(Context context) {        return Settings.Secure.getString(                context.getContentResolver(), Settings.Secure.ANDROID_ID);    }

转载请说明出处:http://blog.csdn.net/u012438830/article/details/78416585