How to Give Multiple Permissions In Andorid Dynamically

Posted By : Akash Tomer | 22-Feb-2018

In Android, we have to give the permission to access the sandbox(Memory area) of another app. Before API 21 we just need to declare the permission in the android manifest file. For API 21 or higher we need to give the permission dynamically. In this tutorial, I am going to explain how to give multiple permissions in one go.

Just make a permission utils class which check your device version and where you can pass all the permission to access any resource of a device.There is a method checkPermission where you have to pass the array of permission, a message for alert dialog and the request code.

public class PermissionUtils {
 Context context;
 Activity current_activity;
 PermissionResultCallback permissionResultCallback;
 ArrayList<String> permission_list = new ArrayList<>();
 ArrayList<String> listPermissionsNeeded = new ArrayList<>();
 String dialog_content = "";
 int req_code;

 public PermissionUtils(Context context) {
 this.context = context;
 this.current_activity = (Activity) context;
 permissionResultCallback = (PermissionResultCallback) context;
 }
 
 public void checkPermission(ArrayList<String> permissions, String dialog_content, int request_code) {
 this.permission_list = permissions;
 this.dialog_content = dialog_content;
 this.req_code = request_code;

 if (Build.VERSION.SDK_INT >= 23) {
 if (checkAndRequestPermissions(permissions, request_code)) {
 permissionResultCallback.PermissionGranted(request_code);
 Log.i("all permissions", "granted");
 Log.i("proceed", "to callback");
 }
 } else {
 permissionResultCallback.PermissionGranted(request_code);

 Log.i("all permissions", "granted");
 Log.i("proceed", "to callback");
 }

 }

 private boolean checkAndRequestPermissions(ArrayList<String> permissions, int request_code) {

 if (permissions.size() > 0) {
 listPermissionsNeeded = new ArrayList<>();

 for (int i = 0; i < permissions.size(); i++) {
 int hasPermission = ContextCompat.checkSelfPermission(current_activity, permissions.get(i));

 if (hasPermission != PackageManager.PERMISSION_GRANTED) {
 listPermissionsNeeded.add(permissions.get(i));
 }

 }

 if (!listPermissionsNeeded.isEmpty()) {
 ActivityCompat.requestPermissions(current_activity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), request_code);
 return false;
 }
 }

 return true;
 }

 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
 switch (requestCode) {
 case 1:
 if (grantResults.length > 0) {
 Map<String, Integer> perms = new HashMap<>();

 for (int i = 0; i < permissions.length; i++) {
 perms.put(permissions[i], grantResults[i]);
 }

 final ArrayList<String> pending_permissions = new ArrayList<>();

 for (int i = 0; i < listPermissionsNeeded.size(); i++) {
 if (perms.get(listPermissionsNeeded.get(i)) != PackageManager.PERMISSION_GRANTED) {
 if (ActivityCompat.shouldShowRequestPermissionRationale(current_activity, listPermissionsNeeded.get(i)))
 pending_permissions.add(listPermissionsNeeded.get(i));
 else {
 Log.i("Go to settings", "and enable permissions");
 permissionResultCallback.NeverAskAgain(req_code);
 Toast.makeText(current_activity, "Go to settings and enable permissions", Toast.LENGTH_LONG).show();
 return;
 }
 }

 }
 if (pending_permissions.size() > 0) {
 showMessageDialog(dialog_content,
 new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {

 switch (which) {
 case DialogInterface.BUTTON_POSITIVE:
 check_permission(permission_list, dialog_content, req_code);
 break;
 case DialogInterface.BUTTON_NEGATIVE:
 Log.i("permisson", "not fully given");
 if (permission_list.size() == pending_permissions.size())
 permissionResultCallback.PermissionDenied(req_code);
 else
 permissionResultCallback.PartialPermissionGranted(req_code, pending_permissions);
 break;
 }


 }
 });

 }
 else {
 Log.i("all", "permissions granted");
 Log.i("proceed", "to next step");
 permissionResultCallback.PermissionGranted(req_code);

 }


 }
 break;
 }
 }


 private void showMessageDialog(String message, DialogInterface.OnClickListener okListener) {
 new AlertDialog.Builder(current_activity)
 .setMessage(message)
 .setPositiveButton("Ok", okListener)
 .setNegativeButton("Cancel", okListener)
 .create()
 .show();
 }
}

After that, it will check the API version of your phone If API version is 21 or higher than it will show a dialog of permission. If the user accepts the permission then it onPermissionGranted() is called in activity. If a user accepts the partial conditions then PartialPermissionGranted() will be called and alert is shown that why this permission is necessary for the application. If user denied all permission then it's PermissionDinied() is called.

Now just implement the PermissionResultCallback interface for all callback in your activity class.

Hope this will help you.

 

About Author

Author Image
Akash Tomer

Akash is an Android Developer at Oodles Technology.

Request for Proposal

Name is required

Comment is required

Sending message..