yanzhenjie / AndPermission

:strawberry: Permissions manager for Android platform.
https://yanzhenjie.com/AndPermission
Apache License 2.0
6.63k stars 1.1k forks source link

2.0.3 ACCESS_BACKGROUND_LOCATION 在android 9上永远回调失败 #528

Open KittenBall opened 5 years ago

KittenBall commented 5 years ago

虽然在android9上没有申请ACCESS_BACKGROUND_LOCATION的可能,但Permission.Group.Location权限组内定义了后台位置信息权限,所以在android9.0的手机上申请位置信息权限组会永远失败 具体是在StandardChecker的hasPermission方法内return false

@Override
    public boolean hasPermission(Context context, List<String> permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true;

        AppOpsManager opsManager = null;
        for (String permission : permissions) {
            int result = context.checkPermission(permission, android.os.Process.myPid(), android.os.Process.myUid());
            if (result == PackageManager.PERMISSION_DENIED) {
                return false;
            }

            String op = AppOpsManager.permissionToOp(permission);
            if (TextUtils.isEmpty(op)) {
                continue;
            }

            if (opsManager == null) opsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            result = opsManager.checkOpNoThrow(op, android.os.Process.myUid(), context.getPackageName());
            if (result != AppOpsManager.MODE_ALLOWED && result != MODE_ASK && result != MODE_COMPAT) {
                return false;
            }
        }
        return true;
    }
//result = -1
int result = context.checkPermission(permission, android.os.Process.myPid(), android.os.Process.myUid());

所以对于后台位置信息权限要做兼容,也许最简单的处理方式是

public static final String[] LOCATION = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? new String[]{Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION,
            Permission.ACCESS_BACKGROUND_LOCATION} : new String[]{Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION};

😅

另外,也许对于Android10 应该分为两个权限组,一个不包含后台位置权限,一个包含后台位置权限,因为对部分应用而言,申请位置信息权限组也仅仅只是在前台使用,也应该只在前台使用,何况Android Q对使用后台位置信息的应用会在通知栏显示,就更不应该毫无理由的使用后台权限了,只是个建议 😋

Roben1016 commented 4 years ago

我也遇到了, 6.0以上 10.0以下都是这样, 单独用hasPermissions判断是否有定位权限用Permission.Group.LOCATION直接就返回false