How to check the multiple permission at single request in Android M?
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.
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;
}
![Zala Janaksinh](https://i.stack.imgur.com/TnIZc.jpg?s=256&g=1)
Zala Janaksinh
Gold medalist in PGDCA && Completed the MCA with first class from Gujarat Vidhyapith Ahamedabad. Now work as Full Stack Mobile Developer. Like Android,React-Native , Flutter & iOS & also work with Php + Python
Updated on July 05, 2022Comments
-
Zala Janaksinh about 2 years
I want to use the
- android.permission.CAMERA
- 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 over 8 yearsPlease 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 almost 8 yearsbut you check your code? In this code also show native permission dialog multiple time.
-
velval almost 8 years@Zala Janaksinh yeah...android will display the permissions individually but you make just a single request.
-
Zala Janaksinh almost 8 yearsI know that,so it's my problem. bro.. may be android resolved this in next update.
-
dipali almost 8 yearsif you are using marshmellow then you have to use these separate permission
-
zyamys about 7 yearsIt's pretty sketchy to rely on a magic number (PERMISSION_GRANTED = 0).
-
Federico Alvarez almost 7 yearsAwesome! Thanks!
-
Yonjuni over 6 yearsI had to add
this
, otherwise it wouldn't workActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE, ...} ,ASK_MULTIPLE_PERMISSION_REQUEST_CODE);
-
kyawagwin over 6 years@Yonjuni it was not checking the the list of permissions whether is it granted or not...
-
M. Usman Khan over 6 yearsquite untidy way for requesting multiple permissions.
-
mohit over 6 yearslet me know why?
-
Ṃųỻịgǻňạcểơửṩ about 6 yearsA link-only answer is not recommended. It is liable to downvotes, which is only just a warning, and will eventually get deleted.
-
sajjad Yosefi about 4 yearsHe did not say to introduce a library
-
Čamo about 4 yearsIf I can, why this condition tests only index 0?
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
-
Umberto Migliore almost 4 yearsthe onrequestpermission doesn't work because check only the first granResult...
-
Annie over 3 yearsAwesome answer ! Worked perfect
-
Ahmed Elsayed almost 3 yearsThis 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 about 2 yearsWhat is the value of
MULTIPLE_PERMISSIONS
?