我的程序一次只能请求1个权限

时间:2018-02-07 20:06:27

标签: android android-studio permissions

此程序需要访问相机和位置。但是,当设备需要同时请求时,它只请求其中一个权限,并且在我关闭程序然后重新打开之前不会请求另一个权限。我无法弄清楚这个问题。

StringIO

有人请帮忙!顺便说一句,我有意地从这个问题中省略了shouldShowRequestPermissionsRationale,以及onRequestPermissionsResult方法,因为我认为那些与问题无关。 (我甚至尝试在onResume()中调用request(),但是当一个权限被拒绝并且用户检查了“#34;不再显示"”时,这会导致无限循环。我完全没有想法。

4 个答案:

答案 0 :(得分:1)

另一个有效的解决方案是使用RxJava和RxPermission

所以你可以这样做:

res.write(data);
res.end();

或者您可以请求合并的每个权限:

rxPermissions
    .requestEach(Manifest.permission.CAMERA,
             Manifest.permission.ACCESS_FINE_LOCATION)
    .subscribe(permission -> { // will emit 2 Permission objects
        if (permission.granted) {
           // `permission.name` is granted !
        } else if (permission.shouldShowRequestPermissionRationale) {
           // Denied permission without ask never again
        } else {
           // Denied permission with ask never again
           // Need to go to the settings
        }
    });

答案 1 :(得分:1)

使用Ted权限:

dependencies {
    compile 'gun0912.ted:tedpermission:2.1.0'
}

制作PermissionListener

PermissionListener permissionlistener = new PermissionListener() {
    @Override
    public void onPermissionGranted() {
        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPermissionDenied(ArrayList<String> deniedPermissions) {
        Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
    }


};

启动TedPermission

//Change your permission in this section, you can add any number of permissions\\
// You can also check whether user has given the permission or not \\

TedPermission.with(this)
    .setPermissionListener(permissionlistener)
    .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
    .setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
    .check();

欲了解更多信息,请访问

https://github.com/ParkSangGwon/TedPermission

希望它有所帮助。

答案 2 :(得分:0)

请求两种权限的方法必须是requestBoth()

protected void requestBoth() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA}, MULT_KEY);
    }

将您的代码更改为:

 if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
         //requestUpdates();
          requestBoth();

    } 
    ...

答案 3 :(得分:0)

现在,您可以像这样将它们全部放在一个String数组中

 ActivityCompat.requestPermissions((Activity) Login.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_NUMBERS, Manifest.permission.READ_SMS}, 100);