Android Q 適配踏坑

Android Q 適配開始

一、應用讀取 Device ID
項目 targetSdkVersion爲 29時 獲取設備唯一標識時
SecurityException: getUniqueDeviceId: The user 10279 does not meet the requirements to access device identifiers.

在Android Q 之前有如下代碼,獲取設備Id,IMEI等
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceID = telephonyManager.getDeviceId();
在Android Q之後以及改變

 /**
     * 獲取設備號
     *  Android Q 無法再獲取到 telephonyManager.getDeviceId()
     * @return
     */
    public static String getDivicesNumbr(Context context) {
        String deviceID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (TextUtils.isEmpty(deviceID)) {
            deviceID = getUniquePsuedoID();
        }
        return deviceID;
    }
/**
 * 得到隨機 UUID
 */
public static String getUUID() {
    String uuid = SpUtils.getString("uuid", "");

    if (TextUtils.isEmpty(uuid)) {
        uuid = UUID.randomUUID().toString();
        SpUtils.putString("uuid", uuid);
    }
    PLog.e("getDeviceId :", "getUUID : " + uuid);
    return uuid;
}

public static String getUniquePsuedoID() {
    String serial = null;

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

            Build.CPU_ABI.length() % 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 位

    try {
        serial = android.os.Build.class.getField("SERIAL").get(null).toString();
        //API>=9 使用serial號
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    } catch (Exception exception) {
        //serial需要一個初始化
        // 隨便一個初始化
        serial = "serial";
    }
    //使用硬件信息拼湊出來的15位號碼
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}

二、Android Q手機在後臺無法正常定位問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章