Now you can Subscribe using RSS

Submit your Email

Sunday

Request multiple runtime persimmsions in android marshmallow at once example

Vishal Shrestha







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 M:

  • 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
};

  • 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;
        }
  • 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); } 
     
  • Handling the response in onRequestPermissionsResult:
  • @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case PERMISSION_ALL: { Map 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.RECEIVE_BOOT_COMPLETED, 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.RECEIVE_BOOT_COMPLETED) == PackageManager.PERMISSION_GRANTED) { Log.d("mylog", "service permission granted"); if (hasPermissions(LauncherActivity.this, PERMISSIONS)) startMainActivity(); // process the normal flow //else any one or both the permissions are not granted } else { Toast.makeText(LauncherActivity.this, "Please grant all the required permissions", Toast.LENGTH_LONG).show(); } return; }
      }
     } 
    }
    There you have it, it's not difficult to request runtime permissions! if you've any problems, do comment below or contact me in any social media. Happy coding for Marshmallow and beyond!

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha