Android-关于设备唯一ID的奇技淫巧

语言: CN / TW / HK

小知识,大挑战!本文正在参与「程序员必备小知识」创作活动

本文已参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金。

前言

最近在二开项目国际版客户的功能,我们项目中默认是有一个游客登录的,一般大家都是取Android设备的唯一ID上传服务器,然后服务器给你分配一个用户信息.但是Google在高版本对于设备唯一Id的获取简直限制到了极点.

以前我都是直接获取IMEI来作为设备的唯一标识 var imei: String = "" val tm: TelephonyManager = context.getSystemService(Service.TELEPHONY_SERVICE) as TelephonyManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { imei = tm.imei } else { imei = tm.deviceId } Log.e("TAG","$imei") imei和deviceId都有一个重载函数,主要是区别双卡的一个情况

image.png

Android6.0以后我们加一个动态权限即可,但是用户只要拒绝就没办法获取了,不过一般来说我们会有个弹框来引导用户同意 <uses-permission android:name="android.permission.READ_PHONE_STATE"/> Android 10.0 谷歌再一次收紧权限

image.png

<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" /> 如果你把他放到AndroidManifest会报错

image.png

官方也说了,你要是弟弟(9.0 以下)我给你报null,你要是10.0 还敢用我就直接抛异常. 后面在stackoverflow上面找到了一个办法

``` 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 () {
                            uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
                        } else {
                            @SuppressLint("MissingPermission") 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.
 *

 *
 * @return a UUID that may be used to uniquely identify your device for most purposes.
 */
public String getDeviceUuid() {
    return uuid.toString();
}

} ```

这个类的意思是,首先他会去SharedPreferences查询有没有,没有的话再去查询ANDROID_ID,后面判断了是否是9774d56d682e549c,因为有的厂商手机好多ANDROID_ID都是这个,所以判断一下,防止好几万个人用一个账号,不然那就笑嘻嘻了,后面如果真等于9774d56d682e549c了,就通过下面的 @SuppressLint("MissingPermission") final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();

来获取DeviceId,但是这个AndroidId虽然可以是获取了,但是会受限于签名文件,如果在相同设备上运行但是应用签名不一样,获取到的ANDROID_ID就会不一样,比如谷歌商店会二次签名apk,他获取的id可能就是159951,后面我们要测试时,上传到内部测试的包好像会再次签名,这次获取的可能是951159,然后我们用android提供的签名文件可能就是147258,我们自己新建一个签名文件就可能是258369,总之这个ANDROID_ID会受制于签名文件

反正最后我们国际版用到了Mob的推送服务,推送中有一个只推送单个设备,然后我们就设想,直接用Mob的唯一设备Id和我们服务器绑定如何,后面一经测试,效果很好,直接跳过大堆测试和寻找时间

``` //阿里云唯一设备id val deviceId = PushServiceFactory.getCloudPushService().deviceId

//Mob CloudPushService pushService = PushServiceFactory.getCloudPushService(); pushService.register(applicationContext, new CommonCallback() { @Override public void onSuccess(String response) { Log.e("TAG", "onSuccess: "+response); }

@Override
public void onFailed(String errorCode, String errorMessage) {
}

});

//友盟唯一设备ID val pushAgent = PushAgent.getInstance(context) pushAgent.register(object : UPushRegisterCallback { override fun onSuccess(deviceToken: String) { //注册成功会返回deviceToken deviceToken是推送消息的唯一标志 Log.i(TAG, "注册成功:deviceToken:--> $deviceToken") }

override fun onFailure(errCode: String, errDesc: String) {
    Log.e(TAG, "注册失败:--> code:$errCode, desc:$errDesc")
}

}) ```

这是常用的第三方服务获取唯一设备ID的方法,其实有的人可能用的跟我不一样,基本上文档里面都有,真找不到可以去问问客服

终于解决一个让人头疼的问题了,下班,回家