如何一个接一个地请求多个权限? [英] How to ask for multiple permissions one after another?

查看:88
本文介绍了如何一个接一个地请求多个权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要访问 CAMERA WRITE_EXTERNAL_STORAGE 权限.

My app requires to access CAMERA and WRITE_EXTERNAL_STORAGE permissions.

应用加载后,我想让用户一个接一个地允许这两个权限.我有以下代码:

Once my app loads I want to ask user to allow both of these permissions one after another. I have this code:

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
    }


    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);

    }

现在,一旦我的应用程序加载,它会请求第一个权限,但直到我再次重新加载整个应用程序时,才请求第二个权限.

Now once my app loads it asks for the first permission but never asks for the second until I reload the entire app again.

应用加载后,如何向用户要求这两个权限?

How do I ask both of these permissions from the user once app loads?

推荐答案

您必须将所有权限放入一个String数组:

You have to put all of the permissions into one String array:

new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}

这样,将向用户显示一个包含所有权限的对话框,他们可以决定分别拒绝或授予每个权限.完成操作后,您的Activity将被onRequestPermissionsResult()调用.

This way, users will be shown a dialog with all of the permissions and they can decide to deny or grant each permission individually. As soon as they are finished, your Activity will be called with onRequestPermissionsResult().

此方法的参数之一是名为 grantResults 的类型为int的数组,您可以对其进行评估以了解授予了哪些权限.

One of the parameters of this method is an array of type int named grantResults which you can evaluate to know which permissions were granted.

这篇关于如何一个接一个地请求多个权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆