Android 开发版本升级到29以上,获取getDeviceId报错异常,获取失败?

android10系统手机报错:获取不到getDeviceId 这类唯一标识的问题
 getSubscriberId: The user 10268 does not meet the requirements to access device identifiers
调用下面的方法就能正确获取到:
String androidId = PhoneUtils.getDeviceId(mContext);



public static String getDeviceId(Context context) {
    final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
    if (targetSdkVersion > Build.VERSION_CODES.P && Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
        return getUniqueID(context);
    } else {
        return getTelId(context);
    }
}

private static String getTelId(Context context) {
    final TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return null;
    }
    return manager.getDeviceId();
}

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(context);
    }

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

private static String getUUID(Context context) {
    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) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return null;
                }
                serial = 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 = Build.UNKNOWN + UUID.randomUUID().toString(); // 随便一个初始化
    }

    //使用硬件信息拼凑出来的15位号码
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}

这样就能解决获取不到getDeviceId的问题,我遇到的是这种情况,已经解决,如果有其它问题,欢迎各位大佬指点

猜你喜欢

转载自blog.csdn.net/qq_25462179/article/details/117529460