Check Multiple Marshmallow Runtime Permissions Android Programmatically

Hi, Guys. Welcome to check multiple marshmallow runtime permissions in Android studio example.

In this tutorial, We will learn how to programmatically request multiple marshmallow runtime permissions android studio.

From android’s Marshmallow version, the developer needs to programmatically check and request multiple runtime permissions android to the user.

What is Runtime Permission In Android?

Runtime Permissions means that permissions required by the android app for it’s several operations.

For example, if your app wants to capture images, you need to use camera of the android device. You need to have permission for using the camera of the device.

The primary purpose of the runtime permission is to protect the user’s data and personal information such as contact list and SMS.

Runtime permissions provides extra security to the user.

Earlier, developer do not need to ask for these permissions.

App was allowed to use device resources without user’s permission which was not an efficient thing in terms of user’s private security.

You need user agreement while working with dangerous Permissions only. Normal, Signature and Special are the other types of the permissions. Other permissions can be used without runtime environment.

Using this example, you can ask for multiple or single runtime permissions Android as per your requirements.

Following is the output:

Understanding Runtime Permissions Architecture

Let us understand how whole architecture of Runtime Permission works.

  • As you have seen in the video, first of all, a dialog is created for asking each and every permission.
  • If the user denies specific permission, compiler will again ask for that particular permission when the user opens an Android app. This process continuous until user grants that permission.
  • Once user allows, it will never ask for the same permission in the future.
  • There is also one checkbox saying “Never ask again,” in the permission dialog. If the user checks this checkbox and then denies, then Android will never ask for that permission.

Now how to ask for that permission?

  • For handling above situation, We need to open another dialog which allows the user to go to setting of our app and from there, he can turn on and off various permissions.

Following are the required code snippets.

Step 1: Creating WelcomeActivity.java class:

Create new Activity named WelcomeActivity.java and add below code

User will only enter in this activity when he has allowed all the runtime permissions. Otherwise compiler will close the app.

Update activity_welcome.xml as below

Step 2: Creating MainActivity.java class:

Step 3: Description of MainActivity

Defining permission codes

In checkAndRequestPermissions() method, create permission code for each permission like below.

These integer variables will represent the specific type of permissions in the whole example.

Preparing ArrayList

Add each permission in ArrayList and ask user by below source code

listPermissionsNeeded will contain all the permissions which we are going to ask from the user.

Hashmap Creation

In onRequestPermissionResult() method, put all the permission in one Hashmap.

Now check if user have granted all the required permissions or not.

If user have granted all the required permissions then open WelcomeActivity or continue to the normal flow of your app.

If not granted, then ask again with your message.

Above source code will check if user gave granted all the permissions or not.

If any of the permissions is missing then again this code will request for runtime permissions.

Update Package name

When the user have denied any permission and he has also checked the “”Never ask again,” checkbox then the compiler will call explain() method.

In explain() method you need to update package name in below line

Define required permissions in AndroidManifest.xml

Other Scenario to handle

In the above code, we ask check multiple marshmallow runtime permissions in splash screen.

But sometimes, you want to ask for specific permission in specific activity.

You may want to ask for a different number of permissions in different activities.

To handle this situation you just need to change a number of permissions in following snippets.

  1. When creating permissions codes

     
  2. Create ArrayList of permissions with below one
  3. While creating HashMap

     
  4. In following two if conditions

    and

     

Three Protection levels

Permissions are divided in the three levels of security.

  1. Normal Permissions
  2. Signature Permissions
  3. Dangerous Permissions

Normal Permissions

When the app requires the data or resource which falls outside the app’s sandbox, there is very low risk to the user’s security or the operations of the other app.

These permissions are known as Normal Permissions.

Normal permissions are not much harmful to the privacy of the user. Hence, no need to ask them at runtime to the user.

For example, app needs to use the internet. Here, accessing internet via app is not a big task and it will not fetch user’s personal data. So INTERNET permission is a normal permission.

Signature Permissions

The system grants these app permissions at install time, but only when the app that attempts to use a permission is signed by the same certificate as the app that defines the permission.

For example, CLEAR_APP_CACHE permission.

Dangerous Permissions

These are the permissions which are sensitive to the use’s personal data and security. These permissions can also alter the device’s data storage or the operation of any other apps.

For example, you want to save the picture on device’s internal storage (WRITE_EXTERNAL_STORAGE).

User has to personally grant these permissions.

If user does not grant these permissions, your app can not provide the functionalities which require dangerous permissions.

Special Permissions

There are two permissions which are not included in the normal or dangerous permissions.

SYSTEM_ALERT_WINDOW and WRITE_SETTINGS are special permissions.

You can find some more details and list of all the permissions here on an official document.

Kotlin Version

Kotlin version of this tutorial is : Android Kotlin Runtime Permissions example.

Other charming tutorials you will definitely like

That’s all for check request multiple marshmallow runtime permissions android studio tutorial.

If you have any question about implementing check request multiple marshmallow runtime permissions android, feel free to ask in the comment section.

Cheers and happy coding!

Download Source Code For Check Multiple Marshmallow Runtime Permissions Android

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

10 Responses

  1. Sunder says:

    Nice one example

  2. John says:

    I need to draw/mark a path on google maps… I was thinking of sending my location every 30-60seconds and drawing my path like that, my doubt is, how can i mark the path…

    Using volley to send data to mysql and then retrieving my points to a map …

    But how can i mark the path im going (like a hicking app) and draw it on google maps? any tips ?
    Thank you

  3. Shailendra says:

    Great work, Thankful to you for this work that helped me as beginner….

  4. Muhammad Umair says:

    not work

  5. Nisha Ashok says:

    Hi, how can we handle following case –

    Step 1: Opened app and gave all the necessary permissions

    Step 2: Clicked Home button(So the app is in background)

    Step 3: Manually changed the permissions in the Settings

    Step 4: Launched the app from multitasks, now it crashes because of app context becomes invalid

  6. Nisha says:

    Hi, how can we handle following case –

    Step 1: Opened app and gave all the necessary permissions

    Step 2: Clicked Home button(So the app is in background)

    Step 3: Manually changed the permissions in the Settings

    Step 4: Launched the app from multitasks, now it crashes because of app context becomes invalid

    • Hardik Parsania says:

      Good detailed comment!

      When you launched the app from multitask it will run onResume() method as per android activity life cycle.
      In this tutorial i have not override onResume() method, but for your case you need to do this.
      In onResume() method, try by defining another context and use that context where ever needed.
      Ping me back with your results.

Leave a Reply

Your email address will not be published. Required fields are marked *