Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask Multiple Permissions 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();
}
like image 321
shivajichandran Avatar asked Aug 02 '17 17:08

shivajichandran


2 Answers

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);
like image 101
Aman Grover Avatar answered Sep 29 '22 12:09

Aman Grover


Why do you want to request permissions multiple times. The requestpermission method accepts array of Permissions. 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. so here you will have to handle your check whether user granted the app the permission. If yes you can continue with your logic

like image 25
Kapil G Avatar answered Sep 29 '22 13:09

Kapil G