How to check the multiple permission at single request in Android M?

123,962

Solution 1

You can ask multiple permissions (from different groups) in a single request. For that, you need to add all the permissions to the string array that you supply as the first parameter to the requestPermissions API like this:

requestPermissions(new String[]{
                                Manifest.permission.READ_CONTACTS,
                                Manifest.permission.ACCESS_FINE_LOCATION},
                        ASK_MULTIPLE_PERMISSION_REQUEST_CODE);

On doing this, you will see the permission popup as a stack of multiple permission popups. Ofcourse you need to handle the acceptance and rejection (including the "Never Ask Again") options of each permissions. The same has been beautifully explained over here.

Solution 2

First initialize permission request code

public  static final int PERMISSIONS_MULTIPLE_REQUEST = 123;

Check android version

 private void checkAndroidVersion() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkPermission();

    } else {
        // write your logic here
    }

}

check multiple permission code

 private void checkPermission() {
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.READ_EXTERNAL_STORAGE) + ContextCompat
            .checkSelfPermission(getActivity(),
                    Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale
                (getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) ||
                ActivityCompat.shouldShowRequestPermissionRationale
                        (getActivity(), Manifest.permission.CAMERA)) {

      Snackbar.make(getActivity().findViewById(android.R.id.content),
                    "Please Grant Permissions to upload profile photo",
                    Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            requestPermissions(
                                    new String[]{Manifest.permission
                                            .READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
                                    PERMISSIONS_MULTIPLE_REQUEST);
                        }
                    }).show();
        } else {
            requestPermissions(
                    new String[]{Manifest.permission
                            .READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
                    PERMISSIONS_MULTIPLE_REQUEST);
        }
    } else {
        // write your logic code if permission already granted
    }
}

call back method after grant permission by user

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions, @NonNull int[] grantResults) {

    switch (requestCode) {
        case PERMISSIONS_MULTIPLE_REQUEST:
            if (grantResults.length > 0) {
               boolean cameraPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
               boolean readExternalFile = grantResults[0] == PackageManager.PERMISSION_GRANTED;

                if(cameraPermission && readExternalFile)
                {
                    // write your logic here 
                } else {
                    Snackbar.make(getActivity().findViewById(android.R.id.content),
                        "Please Grant Permissions to upload profile photo",
                        Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                requestPermissions(
                                        new String[]{Manifest.permission
                                                .READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
                                        PERMISSIONS_MULTIPLE_REQUEST);
                            }
                        }).show();
                }
           }
           break;
    }
}

Solution 3

I had the same issue and stumbled on this library.

Basically you can ask for multiple permissions sequentially, plus you can add listeners to popup a snackbar if the user denies your permission.

dexter_sample

Solution 4

As said earlier, currently every permission group has own permission dialog which must be called separately.

You will have different dialog boxes for each permission group but you can surely check the result together in onRequestPermissionsResult() callback method.

Here is a working example link, may be useful for someone.

Solution 5

For multiple permission you can use this code :

final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;

private void insertDummyContactWrapper() {
    List<String> permissionsNeeded = new ArrayList<String>();

    final List<String> permissionsList = new ArrayList<String>();
    if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
        permissionsNeeded.add("GPS");
    if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS))
        permissionsNeeded.add("Read Contacts");
    if (!addPermission(permissionsList, Manifest.permission.WRITE_CONTACTS))
        permissionsNeeded.add("Write Contacts");

    if (permissionsList.size() > 0) {
        if (permissionsNeeded.size() > 0) {
            // Need Rationale
            String message = "You need to grant access to " + permissionsNeeded.get(0);
            for (int i = 1; i < permissionsNeeded.size(); i++)
                message = message + ", " + permissionsNeeded.get(i);
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                        }
                    });
            return;
        }
        requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
        return;
    }

    insertDummyContact();
}

private boolean addPermission(List<String> permissionsList, String permission) {
    if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
        permissionsList.add(permission);
        // Check for Rationale Option
        if (!shouldShowRequestPermissionRationale(permission))
            return false;
    }
    return true;
}
Share:
123,962
Zala Janaksinh
Author by

Zala Janaksinh

Gold medalist in PGDCA &amp;&amp; Completed the MCA with first class from Gujarat Vidhyapith Ahamedabad. Now work as Full Stack Mobile Developer. Like Android,React-Native , Flutter &amp; iOS &amp; also work with Php + Python

Updated on July 05, 2022

Comments

  • Zala Janaksinh
    Zala Janaksinh about 2 years

    I want to use the

    1. android.permission.CAMERA
    2. android.permission.WRITE_EXTERNAL_STORAGE

    in single request using

    ActivityCompat.requestPermissions(Activity activity,new String permisionList[],int permissionRequestcode);
    

    But my problem is at time I request only one permission, I read about group-permission,But it's work for only Same group which one decided by Developer, Like CONTACT_GROUP : read_contact,write_contact etc.

    I want create the custom group permission which ask me only one request & provide me only one response.

    Thanks

  • Zala Janaksinh
    Zala Janaksinh over 8 years
    Please read my question perfectly.. I want check all permission in single request.. Not multiple dialog. & bro your link is not related with my question... sorry..
  • Zala Janaksinh
    Zala Janaksinh almost 8 years
    but you check your code? In this code also show native permission dialog multiple time.
  • velval
    velval almost 8 years
    @Zala Janaksinh yeah...android will display the permissions individually but you make just a single request.
  • Zala Janaksinh
    Zala Janaksinh almost 8 years
    I know that,so it's my problem. bro.. may be android resolved this in next update.
  • dipali
    dipali almost 8 years
    if you are using marshmellow then you have to use these separate permission
  • zyamys
    zyamys about 7 years
    It's pretty sketchy to rely on a magic number (PERMISSION_GRANTED = 0).
  • Federico Alvarez
    Federico Alvarez almost 7 years
    Awesome! Thanks!
  • Yonjuni
    Yonjuni over 6 years
    I had to add this, otherwise it wouldn't work ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE, ...} ,ASK_MULTIPLE_PERMISSION_REQUEST_CODE);
  • kyawagwin
    kyawagwin over 6 years
    @Yonjuni it was not checking the the list of permissions whether is it granted or not...
  • M. Usman Khan
    M. Usman Khan over 6 years
    quite untidy way for requesting multiple permissions.
  • mohit
    mohit over 6 years
    let me know why?
  • Ṃųỻịgǻňạcểơửṩ
    Ṃųỻịgǻňạcểơửṩ about 6 years
    A link-only answer is not recommended. It is liable to downvotes, which is only just a warning, and will eventually get deleted.
  • sajjad Yosefi
    sajjad Yosefi about 4 years
    He did not say to introduce a library
  • Čamo
    Čamo about 4 years
    If I can, why this condition tests only index 0? if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
  • Umberto Migliore
    Umberto Migliore almost 4 years
    the onrequestpermission doesn't work because check only the first granResult...
  • Annie
    Annie over 3 years
    Awesome answer ! Worked perfect
  • Ahmed Elsayed
    Ahmed Elsayed almost 3 years
    This answer has a bug .. if you denied a permission twice continuously , when you click on enable on Snackbar , Snackbar will appear again and again and again
  • KernelPanic
    KernelPanic about 2 years
    What is the value of MULTIPLE_PERMISSIONS?