Unity杂文——Android设备唯一标识

  1. 简介
  2. 环境
  3. 代码
    1. 引用的库的初始化
    2. 调用

原文地址

简介

获取设备唯一标识的方法。

环境

引用库的github地址:Android_CN_OAID
在Android最外层的bundel.gradle里添加下面依赖

allprojects {
    repositories {
        '''
        maven { url 'https://www.jitpack.io' }
        ...
    }
}

在app或者library的bundel.gradle添加下面依赖

dependencies {
implementation 'com.github.gzu-liyujiang:Android_CN_OAID:<version>'
}

这里换成自己需要的版本

代码

引用的库的初始化

在Application或者Active的onCreate里添加下面代码:

@Override
public void onCreate() {
    super.onCreate();
    if (privacyPolicyAgreed) {
        ...
        DeviceIdentifier.register(<Application>);
        ...
    }
}

这里替换成自己程序的application

调用

需要的变量:

//设备唯一标识
private static String deviceId;
//设备唯一标识文件名字
private static String deviceIdFileName = "all_in_sdk";
//设备唯一标识缓存关键字key
private static String deviceIdKey = "DeviceId";
//设备唯一标识正则
private static final Pattern ANDROID_ID_PATTERN = Pattern.compile("^[0-9a-fA-F]{16}$");

获取的方法:

//获取设备的唯一标识
public static String GetDeviceID() {
    if(m_Activity == null){
        m_Activity = UnityPlayer.currentActivity;
    }
    Application application = m_Activity.getApplication();
    if (application == null)
        return "";
    if (deviceId != null)
        return deviceId;
    deviceId = getSPValue((Context)application, deviceIdKey);
    if (!TextUtils.isEmpty(deviceId))
        return deviceId;
    deviceId = getAndroidIdAsDeviceId((Context)application);
    if (!TextUtils.isEmpty(deviceId)) {
        saveSPValue((Context)application, deviceIdKey, deviceId);
        return deviceId;
    }
    deviceId = generateSoftDeviceId();
    if (!TextUtils.isEmpty(deviceId)) {
        saveSPValue((Context)application, deviceIdKey, deviceId);
        return deviceId;
    }
    return deviceId;
}
private static String getSPValue(Context paramContext, String paramString) {
    SharedPreferences sharedPreferences = paramContext.getSharedPreferences(deviceIdFileName, 0);
    return sharedPreferences.getString(paramString, null);
}

private static String getAndroidIdAsDeviceId(Context paramContext) {
    String str;
    if(DeviceID.supportedOAID(m_Activity))
    {
        str = DeviceIdentifier.getOAID(m_Activity);
        if (isLegalAndroidId(str, true))
            return str;
    }
    str = DeviceIdentifier.getAndroidID(m_Activity);
    if (isLegalAndroidId(str, true))
        return str;
    str = DeviceIdentifier.getIMEI(m_Activity);
    if (isLegalAndroidId(str, true))
        return str;
    str = DeviceIdentifier.getWidevineID();
    if (isLegalAndroidId(str, true))
        return str;
    str = DeviceIdentifier.getPseudoID();
    if (isLegalAndroidId(str, true))
        return str;
    str = DeviceIdentifier.getGUID(m_Activity);
    if (isLegalAndroidId(str, true))
        return str;
    return null;
}

private static String generateSoftDeviceId() {
    String str1 = Build.SERIAL;
    String str2 = !TextUtils.isEmpty(str1) ? str1 : "NA";
    try {
        long l1 = 1152921504606846976L;
        long l2 = randomLong(Long.MAX_VALUE - l1) + l1;
        return String.format("%1$s_%2$s", new Object[] { Long.toHexString(l2), str2 });
    } catch (Throwable throwable) {
        return String.format("%1$s_%2$s", new Object[] { "NA" + Long.toHexString(System.currentTimeMillis()), str2 });
    }
}

private static void saveSPValue(Context paramContext, String paramString1, String paramString2) {
    SharedPreferences sharedPreferences = paramContext.getSharedPreferences(deviceIdFileName, 0);
    sharedPreferences.edit().putString(paramString1, paramString2).apply();
}
private static long randomLong(long paramLong) {
    if (Build.VERSION.SDK_INT >= 21)
        return ThreadLocalRandom.current().nextLong(paramLong);
    return (long)((new Random()).nextDouble() * (paramLong - 1L));
}
private static boolean isLegalAndroidId(String paramString, boolean paramBoolean) {
    if (paramBoolean)
        return (!TextUtils.isEmpty(paramString) && ANDROID_ID_PATTERN
                .matcher(paramString).find());
    return (!TextUtils.isEmpty(paramString) && ANDROID_ID_PATTERN
            .matcher(paramString).find());
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 841774407@qq.com

×

喜欢就点赞,疼爱就打赏