Android:要求运行时权限运行第一个权限请求但跳过其他权限

时间:2017-08-02 19:48:43

标签: android runtime-permissions

如果我在permissions上一次要求多个Androidrun time permissions效果很好,并要求用户提供我请求的所有权限。那就是如果我在一个电话中完成所有这些操作。

如果我选择将run time permission个请求分成组,那么我首先请求访问contacts,然后请求访问SMS,我的程序将忽略SMS的请求{1}}并最终将SMS请求视为已拒绝。为什么是这样?程序在等待用户响应的同时继续执行代码吗?如何让我的permission requests分开并且仍然可以在彼此之后立即请求权限?

我之所以不想立即执行所有权限,是因为我只想在需要时请求权限。这就是为什么我将权限分成组。

非常感谢任何帮助!

编辑: 示例代码:调用checkPermissions

public boolean checkPermissions(int permissionsToCheck)
{
    // Use constants above to specify permissions check to do
    if (Build.VERSION.SDK_INT < 23) {
        //Do not need to check the permission
        return true;
    } else {
        switch (permissionsToCheck) {
            case PERMISSIONS_FOR_MESSAGING:
                return ( checkAndRequestContactPermissions() && checkAndRequestSMSPermissions() );
            case PERMISSIONS_FOR_BLUETOOTH:
                return checkAndRequestBluetoothPermissions();
            default:
            return false;
        }

    }
}

致电checkAndRequestContactPermissions()checkAndRequestSMSPermissions()

private boolean checkAndRequestContactPermissions() {
    int permissionReadContacts = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_CONTACTS);
    int permissionWriteContacts = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_CONTACTS);

    List<String> listPermissionsNeeded = new ArrayList<>();
    if (permissionReadContacts != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
    }
    if (permissionWriteContacts != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.WRITE_CONTACTS);
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(mActivity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MY_PERMISSIONS_REQUEST_CONTACTS);
        return false;
    }

    return true;
}

private boolean checkAndRequestSMSPermissions() {
    int permissionReadSMS = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_SMS);
    int permissionSendSMS = ContextCompat.checkSelfPermission(mContext, Manifest.permission.SEND_SMS);

    List<String> listPermissionsNeeded = new ArrayList<>();
    if (permissionReadSMS != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
    }
    if (permissionSendSMS != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.READ_SMS);
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(mActivity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MY_PERMISSIONS_REQUEST_SMS);
        return false;
    }

    return true;
}

我尝试检查我是否拥有“通讯录”和“短信”的权限,但它只请求“通讯录”的权限,而不是两者。

0 个答案:

没有答案