Requesting Runtime Permission – Part 2
We have discussed about Requesting Runtime Permission in last tutorial, but that was for single permission. Today we will discuss about asking for multiple permissions.
Here is some code snippet to take multiple permissions at a time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
List permissionsRequired = new ArrayList(); final List<String> permissionsList = new ArrayList<String>(); if (!checkPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE)) permissionsRequired.add("Read External Storage"); if (!checkPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE)) permissionsRequired.add("Write External Storage"); if (!checkPermission(permissionsList, Manifest.permission.WRITE_CONTACTS)) permissionsRequired.add("Write Contacts"); if (permissionsList.size() > 0) { if (permissionsRequired.size() > 0) { // Need Rationale String message = "You need to grant access to " + permissionsRequired.get(0); for (int i = 1; i < permissionsRequired.size(); i++) message = message + ", " + permissionsRequired.get(i); showCustomDialog(message, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), 11); } }); return; } requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), 11); return; } |
Here is custom checkPermission()
1 2 3 4 5 6 7 8 9 |
private boolean checkPermission(List permissionsList, String permission) { if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { permissionsList.add(permission); // Check for Rationale Option if (!shouldShowRequestPermissionRationale(permission)) return false; } return true; } |
When every single permission got its grant result, the result will be sent to the same callback method, onRequestPermissionsResult().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 11: Map<String, Integer> perms = new HashMap<String, Integer>(); // Initial perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.WRITE_CONTACTS, PackageManager.PERMISSION_GRANTED); // Fill with results for (int i = 0; i < permissions.length; i++) perms.put(permissions[i], grantResults[i]); // Check for ACCESS_FINE_LOCATION if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED perms.get(Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED) { // All Permissions Granted Toast.makeText(MainActivity.this, "All permissions Granted.", Toast.LENGTH_SHORT) .show(); } else { // Permission Denied Toast.makeText(MainActivity.this, "Some Permission is Denied.", Toast.LENGTH_SHORT) .show(); } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } |
The condition is flexible. You have to set it by your own. In some case, if even one permission is not granted, that feature will be just simply disabled. But in some case, it will still work but with limited feature.
Ravi Rupareliya
He is author of Android Gig. He loves to explore new technologies and have worked on Android, React Native, Action on Google and Flutter.
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019