Android 10适配获取设备信息

}

作者在对待Android 10及以上的用户进行了return " "的操作,但我们在开发的过程中是需要去适配这个型号的用户的,查找了错误描述。

he user 10553 does not meet the requirements to access device identifiers.

通过查看 Google Android开发者官方文档《唯一标识符最佳做法》发现
自 Android 10(API 级别 29)起,您的应用必须是设备或个人资料所有者应用,具有特殊运营商许可,或具有 READ_PRIVILEGED_PHONE_STATE 特权,才能访问不可重置的设备标识符。

Android 10适配获取设备信息_第1张图片

所以我们可以使用官方推荐方法,就是文档中看到的,使用SSAID,实例ID、广告ID,随机生成的ID等。

public final class PhoneUtil {
private PhoneUtil() {
throw new UnsupportedOperationException(“u can’t instantiate me…”);
}

/**

  • Return the unique device id.
  • If the version of SDK is greater than 28, it will return an empty string.

  • Must hold {@code }

  • @return the unique device id
    */
    @SuppressLint(“HardwareIds”)
    @RequiresPermission(READ_PHONE_STATE)
    public static String getDeviceId(Context context) {
    if (Build.VERSION.SDK_INT >= 29) {
    return getUniqueID(context);
    }
    TelephonyManager tm = getTelephonyManager();
    String deviceId = tm.getDeviceId();
    if (!TextUtils.isEmpty(deviceId)) return deviceId;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String imei = tm.getImei();
    if (!TextUtils.isEmpty(imei)) return imei;
    String meid = tm.getMeid();
    return TextUtils.isEmpty(meid) ? “” : meid;
    }
    return “”;
    }

private static String getUniqueID(Context context) {
String id = null;
final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
if (!TextUtils.isEmpty(androidId) && !“9774d56d682e549c”.equals(androidId)) {
try {
UUID uuid = UUID.nameUUIDFromBytes(androidId.getBytes(“utf8”));
id = uuid.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

if (TextUtils.isEmpty(id)) {
id = getUUID();
}

return TextUtils.isEmpty(id) ? UUID.randomUUID().toString() : id;
}

private static String getUUID() {
String serial = null;

String m_szDevIDShort = “35” +
Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

((null != Build.CPU_ABI) ? Build.CPU_ABI.length() : 0) % 10 +

Build.DEVICE.length() % 10 + Build.DISPLAY.length() % 10 +

Build.HOST.length() % 10 + Build.ID.length() % 10 +

Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10 +

Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10 +

Build.TYPE.length() % 10 + Build.USER.length() % 10; //13 位

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
serial = android.os.Build.getSerial();
} else {
serial = Build.SERIAL;
}
//API>=9 使用serial号
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
} catch (Exception exception) {
serial = “serial” + UUID.randomUUID().toString(); // 随便一个初始化
}
} else {
serial = android.os.Build.UNKNOWN + UUID.randomUUID().toString(); // 随便一个初始化
}
//使用硬件信息拼凑出来的15位号码
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}
private static TelephonyManager getTelephonyManager() {
return (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE);
ephonyManager() {
return (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE);

你可能感兴趣的:(android)