Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ask for multiple permissions one after another?

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?

like image 331
AlwaysConfused Avatar asked Apr 18 '16 16:04

AlwaysConfused


People also ask

How do I ask for permission access?

Let's move on. “May I…?” “Can I…?” and “Could I…?” are three of the most common expressions in English used to ask for permission. Regardless of situations: informal, semi-formal, or formal, you are expected to be polite in your choice of words, voice tone, and body language whenever you ask for permission.

How do I request multiple permissions in flutter?

Create a new file called permissions_service. dart and in it, we will make a class that has an instance of PermissionHandler from the package. Then we can add a function that takes in a PermissionGroup to request permission of what we want.

How do you ask permission runtime again if the user deny for the first time react native?

I don't think you should ask permission again if the user denies, all you can do is, don't take the app forward , just show them a toast or a dialog telling them that this permission is required for the app to work correctly and ask them to grant permission in the settings.


2 Answers

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

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

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().

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.

like image 167
Bö macht Blau Avatar answered Oct 29 '22 02:10

Bö macht Blau


You can ask for more than one permission in requestPermissions(). It takes an array of permission names, not just a single permission.

Note, though, that whatever you include in that array will be asked for, even if you already have that permission.

So, you can do something like this:

  private static final String[] PERMS_TAKE_PICTURE={
    CAMERA,
    WRITE_EXTERNAL_STORAGE
  };

  private void gimmePermission() {
    if (!canTakePicture()) {
      ActivityCompat.requestPermissions(this,
        netPermissions(PERMS_TAKE_PICTURE), RESULT_PERMS_TAKE_PICTURE);
    }
  }

  private boolean hasPermission(String perm) {
    return(ContextCompat.checkSelfPermission(this, perm)==
      PackageManager.PERMISSION_GRANTED);
  }

  private boolean canTakePicture() {
    return(hasPermission(CAMERA) && hasPermission(WRITE_EXTERNAL_STORAGE));
  }

  private String[] netPermissions(String[] wanted) {
    ArrayList<String> result=new ArrayList<String>();

    for (String perm : wanted) {
      if (!hasPermission(perm)) {
        result.add(perm);
      }
    }

    return(result.toArray(new String[result.size()]));
  }

netPermissions() takes a String[] of the permissions you want and returns a String[] of the permissions that you do not yet hold, which you can then pass along to requestPermissions().

(code derived from this sample app)

like image 37
CommonsWare Avatar answered Oct 29 '22 02:10

CommonsWare