Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 641
Zala Janaksinh Avatar asked Dec 02 '15 10:12

Zala Janaksinh


People also ask

How do I ask for multiple permissions in Android?

In case one or more permissions are not granted, ActivityCompat. requestPermissions() will request permissions and the control goes to onRequestPermissionsResult() callback method. You should check the value of shouldShowRequestPermissionRationale() flag in onRequestPermissionsResult() callback method.

How do you check all permission is granted in Android?

To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.

How do I request runtime permissions in Android?

Requesting Android Runtime PermissionscheckSelfPermission(String perm); It returns an integer value of PERMISSION_GRANTED or PERMISSION_DENIED.

How do you request multiple permissions using RequestMultiplePermissions contract?

How to request multiple permission using RequestMultiplePermissions contract: Process is same for requesting multiple permissions at once. You need to pass array of permission to launch(). ActivityResultCallback will return map with permission as key and its grant status as value e.g. below example prints android.


2 Answers

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.

like image 142
Uncaught Exception Avatar answered Sep 19 '22 21:09

Uncaught Exception


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;     } } 
like image 37
mohit Avatar answered Sep 19 '22 21:09

mohit