`
hyshucom
  • 浏览: 811338 次
文章分类
社区版块
存档分类
最新评论

Android 唯一识别码

 
阅读更多

本文转载自:http://blog.csdn.net/billpig/article/details/6728573

我们在项目过程中或多或少会使用到设备的唯一识别码,我们希望能够得到一个稳定、可靠的设备唯一识别码。今天我们将介绍几种方式。

1. DEVICE_ID

假设我们确实需要用到真实设备的标识,可能就需要用到DEVICE_ID。在以前,我们的Android设备是手机,这个DEVICE_ID可以同通过TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI,MEID或者ESN码,但它在使用的过程中会遇到很多问题:
  • 非手机设备: 如果只带有Wifi的设备或者音乐播放器没有通话的硬件功能的话就没有这个DEVICE_ID
  • 权限: 获取DEVICE_ID需要READ_PHONE_STATE权限,但如果我们只为了获取它,没有用到其他的通话功能,那这个权限有点大才小用
  • bug:在少数的一些手机设备上,该实现有漏洞,会返回垃圾,如:zeros或者asterisks的产品

2. MAC ADDRESS

我们也可以通过手机的Wifi或者蓝牙设备获取MAC ADDRESS作为DEVICE ID,但是并不建议这么做,因为并不是所有的设备都有Wifi,并且,如果Wifi没有打开,那硬件设备无法返回MAC ADDRESS.

3. Serial Number

在Android 2.3可以通过android.os.Build.SERIAL获取,非手机设备可以通过该接口获取。

4. ANDROID_ID

ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,当设备被wipe后该数重置

ANDROID_ID似乎是获取Device ID的一个好选择,但它也有缺陷:

  • 它在Android <=2.1 or Android >=2.3的版本是可靠、稳定的,但在2.2的版本并不是100%可靠的
  • 在主流厂商生产的设备上,有一个很经常的bug,就是每个设备都会产生相同的ANDROID_ID:9774d56d682e549c

5. Installtion ID : UUID

以上四种方式都有或多或少存在的一定的局限性或者bug,在这里,有另外一种方式解决,就是使用UUID,该方法无需访问设备的资源,也跟设备类型无关。

这种方式是通过在程序安装后第一次运行后生成一个ID实现的,但该方式跟设备唯一标识不一样,它会因为不同的应用程序而产生不同的ID,而不是设备唯一ID。因此经常用来标识在某个应用中的唯一ID(即Installtion ID),或者跟踪应用的安装数量。很幸运的,Google Developer Blog提供了这样的一个框架:

public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}


总结

综合以上所述,为了实现在设备上更通用的获取设备唯一标识,我们可以实现这样的一个类,为每个设备产生唯一的UUID,以ANDROID_ID为基础,在获取失败时以TelephonyManager.getDeviceId()为备选方法,如果再失败,使用UUID的生成策略。

重申下,以下方法是生成Device ID,在大多数情况下Installtion ID能够满足我们的需求,但是如果确实需要用到Device ID,那可以通过以下方式实现:

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

public class DeviceUuidFactory {
    protected static final String PREFS_FILE = "device_id.xml";
    protected static final String PREFS_DEVICE_ID = "device_id";

    protected static UUID uuid;



    public DeviceUuidFactory(Context context) {

        if( uuid ==null ) {
            synchronized (DeviceUuidFactory.class) {
                if( uuid == null) {
                    final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0);
                    final String id = prefs.getString(PREFS_DEVICE_ID, null );

                    if (id != null) {
                        // Use the ids previously computed and stored in the prefs file
                        uuid = UUID.fromString(id);

                    } else {

                        final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);

                        // Use the Android ID unless it's broken, in which case fallback on deviceId,
                        // unless it's not available, then fallback on a random number which we store
                        // to a prefs file
                        try {
                            if (!"9774d56d682e549c".equals(androidId)) {
                                uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
                            } else {
                                final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();
                                uuid = deviceId!=null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
                            }
                        } catch (UnsupportedEncodingException e) {
                            throw new RuntimeException(e);
                        }

                        // Write the value out to the prefs file
                        prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit();

                    }

                }
            }
        }

    }


    /**
     * Returns a unique UUID for the current android device.  As with all UUIDs, this unique ID is "very highly likely"
     * to be unique across all Android devices.  Much more so than ANDROID_ID is.
     *
     * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on
     * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back
     * on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a
     * usable value.
     *
     * In some rare circumstances, this ID may change.  In particular, if the device is factory reset a new device ID
     * may be generated.  In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2
     * to a newer, non-buggy version of Android, the device ID may change.  Or, if a user uninstalls your app on
     * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation.
     *
     * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT
     * change after a factory reset.  Something to be aware of.
     *
     * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly.
     *
     * @see http://code.google.com/p/android/issues/detail?id=10603
     *
     * @return a UUID that may be used to uniquely identify your device for most purposes.
     */
    public UUID getDeviceUuid() {
        return uuid;
    }
}

Reference:

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

http://stackoverflow.com/questions/5088474/how-can-i-get-the-uuid-of-my-android-phone-in-an-application


版权所有,转载请出明出处!

分享到:
评论

相关推荐

    UniqueID.java

    android 唯一识别码

    Android设备获取唯一标识码的方法

    Android设备获取唯一标识码的方法,可以用

    获取Android系统唯一识别码的方法

    主要介绍了获取Android系统唯一识别码的方法,涉及通过编程获取Android系统硬件设备标识的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

    Delphi跨平台(Win,Android,IOS)获取设备唯一标识符

    Delphi跨平台(Win,Android,IOS)获取设备唯一标识符ios,android,mswindows获取设备唯一标识符和mac地址。

    获取手机唯一标识

    1、支持Android6.0系统手机不打开权限 2、支持卸载重新安装

    浅谈android获取设备唯一标识完美解决方案

    本文介绍了浅谈android获取设备唯一标识完美解决方案,分享给大家,具体如下: /** * deviceID的组成为:渠道标志+识别符来源标志+hash后的终端识别符 * * 渠道标志为: * 1,andriod(a) * * 识别符来源...

    全志A20 芯片id 获取方法 。

    A20 chipid 获取方法,A20芯片id ,A20 android系统 。A20 串号。唯一ID。

    Android_Get_IMEI.rar

    IMEI是一个缩写,移动设备识别码 (国际移动设备识别码)。这是每个设备的唯一数字序列和特殊字符。这个想法非常类似于网卡的MAC地址。这使得每月在市场上的数百个设备中定位特定设备变得容易。创建Mobile Delphi ...

    Android单片机与蓝牙模块通信实例代码

     蓝牙3.0及以下版本编程需要使用UUID,UUID是通用唯一识别码(Universally Unique Identifier),这是一个软件构建的标准,也是被开源基金会组织应用在分布式计算环境领域的一部分。在蓝牙3.0及下一版本中,UUID被...

    基于蓝牙和语音控制的智能家居系统【100010378】

    相比于现有的智能家居方案,我们的方案通过使用 Android ID 作为手机用户唯一识别码以及语音识别 + 人脸识别双重开门检测,使得整个系统更为安全。同时,我们的方案成本更低,可用于高校学生宿舍推广。 关键词:...

    Android 免密支付+Keystore体系

    首先我们要实现这个功能需要确认几个问题 1.如何创建一个Keystore并保证其唯一性 2.如何设置KeyProtection ...5.当指纹变更之后如何...Android指纹识别,看这一篇就够了 Android 保存私密信息-强大的 keyStore Android K

    信鸽推送代码

    登录后,创建应用,获取应用的唯一识别码AccessKey和密钥SecretKey; 下载对应平台的SDK,并参考开发文档完成SDK集成; 通过API调用或Web业务端方便快速地完成推送测试和实际发送,并实时查看推送效果。 使用场景 ...

    云办公平台快速配置手册

    支持Android、IOS系统硬件特征码绑定,通过ICAB软件读取手机硬件信息,生成唯一特征码,在U1000后台设定用户手机硬件特征码绑定后,用户第一次登录前台,会自动上传唯一特征码,完成手机硬件信息绑定。 6 、证书认证...

    基于NTRIP协议的虚拟差分GPS接收设备设计

    NTRIP是一个开放的,非私有的协议,适合于通过Internet传输GNSS (全球导航卫星系统)数据流及差...在整个系统中,每个参考站都拥有自己的识别码,而且是唯一的。所有挂载在控制中心的参考站都被控制中心记录在一张资源表中。

    网络视听收视大数据技术规范-数据元素集.pdf

    UUID 通用唯一识别码(Universally Unique Identifier) 5 概述 数据元素集为互联网视听平台中各类元数据的集合。 根据网络视听节目收视综合评价需要, 网络视 听收视大数据数据元素集应包括用户基本数据、媒资数据...

    华为C8813解锁

    产品识别码,可以在拨号盘输入: *#*#1357946#*#*,可以看到8位数字识别码。 填完这些资料后,3个工作日内,会有认证码发到你的邮箱。 如果没收到怎么办? 没收到就打电话到华为客服咨询一下吧。华为客服电话: ...

    MYKI Password Manager & Authenticator-crx插件

    您可以使用Android的生物特征认证,iOS和MacOS上的Touch ID以及Windows上的Windows Hello的生物特征认证来解锁MYKI库。 MYKI如何使您的生活更轻松:自动保存和备份您的密码和登录详细信息,通过不存储数据的中继...

Global site tag (gtag.js) - Google Analytics