简体   繁体   English

询问多个权限 Android

[英]Ask Multiple Permissions Android

I'm modifying an existing Face Tracker app Android's Facial Recognition sample projects.我正在修改现有的 Face Tracker 应用程序 Android 的面部识别示例项目。 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();
}

you should have only one String array if you want to ask all permissions in one dialog box, like this:如果您想在一个对话框中询问所有权限,您应该只有一个 String 数组,如下所示:

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);

Why do you want to request permissions multiple times.为什么要多次请求权限。 The requestpermission method accepts array of Permissions. requestpermission 方法接受权限数组。 To request for any permission you can use the below code and add the permissions you need.要请求任何权限,您可以使用以下代码并添加所需的权限。 This is how you handle runtime permissions by requesting them before accessing any data related to permission这是您通过在访问与权限相关的任何数据之前请求它们来处理运行时权限的方式

public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {

    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.CAMERA)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true;
    } else {

        Log.v(TAG,"Permission is revoked");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
        return false;
    }
}
else { //permission is automatically granted on sdk<23 upon installation
    Log.v(TAG,"Permission is granted");
    return true;
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 0:
            boolean isPerpermissionForAllGranted = false;
            if (grantResults.length > 0 && permissions.length==grantResults.length) {
                for (int i = 0; i < permissions.length; i++){
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
                        isPerpermissionForAllGranted=true;
                    }else{
                        isPerpermissionForAllGranted=false;
                    }
                }

                Log.e("value", "Permission Granted, Now you can use local drive .");
            } else {
                isPerpermissionForAllGranted=true;
                Log.e("value", "Permission Denied, You cannot use local drive .");
            }
            if(isPerpermissionForAllGranted){
                shoro();
            }
            break;
        }
    }

Once you do that, for devices with API >=23 You will get popup at runtime and then once the user accepts the permission or rejects it, your onRequestPermissionsResult method is called.一旦你这样做了,对于 API >=23 的设备,你会在运行时弹出窗口,然后一旦用户接受或拒绝它,你的onRequestPermissionsResult方法就会被调用。 so here you will have to handle your check whether user granted the app the permission.所以在这里你必须检查用户是否授予应用程序权限。 If yes you can continue with your logic如果是,你可以继续你的逻辑

What you have to do is just put the permission sin a String array like你所要做的就是把权限 sin 像一个字符串数组

int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
        android.Manifest.permission.READ_CONTACTS,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_FINE_LOCATION
};

and to Check the permission use then然后检查权限使用

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

Now the Last thing to do finally is use it in oncreate() or onStart()现在最后要做的最后一件事是在 oncreate() 或 onStart() 中使用它

if(!hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }

I faced the issue today and answer from Aman Grover actually worked for me, you simple need to do following我今天遇到了这个问题,Aman Grover 的回答实际上对我有用,你只需要做以下事情

final String[] permissions = new String[]{ 
    android.Manifest.permission.READ_CONTACTS, 
    Manifest.permission.READ_EXTERNAL_STORAGE
};

ActivityCompat.requestPermissions(this, permissions, 123);

This is the simplest solution:这是最简单的解决方案:

Add this code in your MainActivity's onCreate method将此代码添加到 MainActivity 的 onCreate 方法中

 //Requesting permissions int PERMISSION_ALL = 1; String[] PERMISSIONS = { android.Manifest.permission.READ_CONTACTS, android.Manifest.permission.RECORD_AUDIO, android.Manifest.permission.CALL_PHONE, }; if (!hasPermissions(this, PERMISSIONS)) { ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL); }

Now, add this function in your main activity class现在,在你的主活动类中添加这个函数

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

You can add ass many permissions you need to ask at the application startup.您可以添加许多在应用程序启动时需要询问的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM