询问多个权限 Android [英] Ask Multiple Permissions Android

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

问题描述

我正在修改现有的 Face Tracker 应用 Android 的面部识别示例项目.我在请求多个永久权限时遇到问题.下面的方法是现有方法的修改版本,它成功地创建了一个弹出窗口来请求相机权限.我正在尝试使用存储权限复制它,但到目前为止我没有成功,我不确定这里需要更改什么.

I'm modifying an existing Face Tracker app Android's Facial Recognition sample projects. I'm having an issue with requesting multiple permanent permissions. The method below is a modified version of the existing method that successfully creates a pop up window to ask for camera permissions. I'm trying to replicate this with the storage permissions but so far I've been unsuccessful and I'm not sure what needs to be changed here.

 private void requestAllPermissions() {
    Log.w(TAG, "Camera + Storage permissions are not granted. Requesting permissions");

    final String[] permissions = new String[]{Manifest.permission.CAMERA};
    final String[] permissions2 = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};


    if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.CAMERA)) {
        ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
        return;
    }

    //new
    if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        ActivityCompat.requestPermissions(this, permissions2, RC_HANDLE_STORAGE_PERM);
        return;
    }


    final Activity thisActivity = this;

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityCompat.requestPermissions(thisActivity, permissions,
                    RC_HANDLE_CAMERA_PERM);
        }
    };

    View.OnClickListener listener2 = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityCompat.requestPermissions(thisActivity, permissions2,
                    RC_HANDLE_STORAGE_PERM);
        }
    };

    Snackbar.make(mGraphicOverlay, R.string.permission_camera_rationale,
            Snackbar.LENGTH_INDEFINITE)
            .setAction(R.string.ok, listener)
            .show();

    Snackbar.make(mGraphicOverlay, R.string.permission_storage_rationale,
            Snackbar.LENGTH_INDEFINITE)
            .setAction(R.string.ok, listener2)
            .show();
}

推荐答案

如果你想在一个对话框中询问所有权限,你应该只有一个 String 数组,像这样:

you should have only one String array if you want to ask all permissions in one dialog box, like this:

int ALL_PERMISSIONS = 101;

final String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

ActivityCompat.requestPermissions(this, permissions, ALL_PERMISSIONS);

这篇关于询问多个权限 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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