Request multiple runtime permissions in android marshmallow at once example

With the release of Android 6.0, named Marshmallow came various new features runtime permissions being a major change. Before Marshmallow, all the permission required to run an app was granted to the app during installation, and mostly users didn’t know what permissions the app was using as they were all granted during installation. With Android 6.0, you need to declare the permissions in the manifest just like in the previous Android versions, but the permissions are not granted during installation, permission are requested to the user when they are required. When the developers use this effectively on Android Marshmallow and higher devices, it can be more informative and intuitive. Runtime permissions give more control to the users over what features the app is allowed to use, for example, if your app needs to use GPS, you need to explicitly ask the user for permission to use GPS. In this post we will learn how to ask for multiple permissions at once so that you don’t irritate user with occasional dialogs requesting permissions.

runtime-permissions-in-android-marshmallow
Runtime Permissions

How to request multiple permissions in Android 6.0 (Marshmallow) at once :

  1. First check if the permissions you need are already granted using checkSelfPermission, if so then continue with the usual methods.
  2. If the permissions are not granted, request the permissions using requestPermissions.
  3. Handle the response of the user in the onRequestPermissionResult callback method.
  4. If the user didn’t grant you the required permission, try explaining why it is needed and then request again.
  5. After you get the permissions, do the needed tasks.

Here is the code snippets for requesting multiple runtime permissions in Android Marshmallow:

1. String Array that contains all the required permissions.

final static String[] PERMISSIONS = { Manifest.permission.WRITE_CALL_LOG,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_CALL_LOG,
            Manifest.permission.INTERNET,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.RECEIVE_BOOT_COMPLETED
};

2. Check if the permissions are already granted using a loop. The following method checks if the permissions are granted: 

public static boolean hasPermissions(Context context, String... permissions) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

3. Calling the method, and if permissions are not granted, requesting for permissions again, I did this in onCreate Method:

if (hasPermissions(this, PERMISSIONS)) { startMainActivity(); } else { ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL); }

4. Handling the response in onRequestPermissionsResult:

@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_ALL: {
Map<String, Integer> perms = new HashMap<>();
// Initialize the map with all permissions
perms.put(Manifest.permission.WRITE_CALL_LOG, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_CALL_LOG, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.INTERNET, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CALL_PHONE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.SEND_SMS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.RECEIVE_BOOT_COMPLETED, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WAKE_LOCK, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.RECEIVE_SMS, PackageManager.PERMISSION_GRANTED);
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (perms.get(Manifest.permission.WRITE_CALL_LOG) == PackageManager.PERMISSION_GRANTED 
                   && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED 
                   && perms.get(Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED 
                   && perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
                   && perms.get(Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED 
                   && perms.get(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED
                   && perms.get(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED 
                   && perms.get(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED 
                   && perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED 
                   && perms.get(Manifest.permission.RECEIVE_BOOT_COMPLETED) == PackageManager.PERMISSION_GRANTED 
                   && perms.get(Manifest.permission.WAKE_LOCK) == PackageManager.PERMISSION_GRANTED) {
Log.d("mylog", "service permission granted");
if (hasPermissions(LauncherActivity.this, PERMISSIONS))
startMainActivity();
// process the normal flow
} else {
Toast.makeText(LauncherActivity.this, "Please grant all the required permissions",
Toast.LENGTH_LONG).show();
}
return;
}
}
}
}

There you have it. We’ve requested multiple permission if the Android Version is Marshmallow or Higher, if you have any problem feel free to comment below! Happy coding.

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Sharing is caring!

Leave a Comment

Your email address will not be published.