Android Request Multiple Runtime Permissions at Single Request in Marshmallow

How to enable different dangerous permissions at once in single dialog on application startup time in android programmatically.

In our previous tutorial we have learn about requesting single runtime permission on application start, but now in this tutorial we would going to Request Multiple Runtime Permissions in our application. This type of request can be made through various types of application who wanted to access different type of content after accessing user permission. So here is the complete step by step tutorial for Android Request Multiple Runtime Permissions at Single Request in Marshmallow.

Read our previous article about requesting single permission.

List of all Runtime Permission in Android Marshmallow :

Permission Group NamePermission Name
CAMERACAMERA
CONTACTSREAD_CONTACTS

WRITE_CONTACTS

GET_ACCOUNTS
MICROPHONERECORD_AUDIO
LOCATIONACCESS_FINE_LOCATION

ACCESS_COARSE_LOCATION
SENSORSBODY_SENSORS
STORAGEREAD_EXTERNAL_STORAGE

WRITE_EXTERNAL_STORAGE
SMSSEND_SMS

RECEIVE_SMS

READ_SMS

RECEIVE_WAP_PUSH

RECEIVE_MMS
PHONEREAD_PHONE_STATE

CALL_PHONE

READ_CALL_LOG

WRITE_CALL_LOG

ADD_VOICEMAIL

USE_SIP

PROCESS_OUTGOING_CALLS
CALENDARREAD_CALENDAR

WRITE_CALENDAR

Contents in this project Android Request Multiple Runtime Permissions at Single Request in Marshmallow :-

  1. Watch the live demo.
  2. Start a new android application project.
  3. Add permission to AndroidManifest.xml file.
  4. Make button in Activity layout.
  5. Declaring button in MainActivity.java file and assign layout ID to button object & create RequestPermissionCode  static variable.
  6. Creating RequestMultiplePermission() method to request multiple permissions.
  7. Creating CheckingPermissionIsEnabledOrNot() method to check permission is already enable or not.
  8. Set setOnClickListener() event on button.
  9. Calling both RequestMultiplePermission() and CheckingPermissionIsEnabledOrNot() inside button click method in IF Else condition.

1. Watch Live demo :


2. Start a new android application project :- After watching the live demo video next step is to start a fresh or open already created project in which you want to implement Request Multiple Runtime Permissions.

3. Add permission to AndroidManifest.xml file :- Open your project’s AndroidManifest.xml and write the permissions like i am requesting 4 types of permissions CAMERA, RECORD_AUDIO, SEND_SMS, GET_ACCOUNTS.

<uses-permission android:name="android.permission.CAMERA"/>

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<uses-permission android:name="android.permission.SEND_SMS"/>

<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

4. Make button in Activity layout :

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Click Here to Enable Multiple Runtime Permission" />

5. Declaring button in MainActivity.java file and assign layout ID to button object & create RequestPermissionCode  static variable :

Button button;
public static final int RequestPermissionCode = 7;

button = (Button)findViewById(R.id.button1);

6. Creating RequestMultiplePermission() method to request multiple permissions :

private void RequestMultiplePermission() {

    // Creating String Array with Permissions.
    ActivityCompat.requestPermissions(MainActivity.this, new String[]
            {
                    CAMERA,
                    RECORD_AUDIO,
                    SEND_SMS,
                    GET_ACCOUNTS
            }, RequestPermissionCode);

}

// Calling override method.
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {

        case RequestPermissionCode:

            if (grantResults.length > 0) {

                boolean CameraPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                boolean RecordAudioPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                boolean SendSMSPermission = grantResults[2] == PackageManager.PERMISSION_GRANTED;
                boolean GetAccountsPermission = grantResults[3] == PackageManager.PERMISSION_GRANTED;

                if (CameraPermission && RecordAudioPermission && SendSMSPermission && GetAccountsPermission) {

                    Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(MainActivity.this,"Permission Denied",Toast.LENGTH_LONG).show();

                }
            }

            break;
    }
}

 7. Creating CheckingPermissionIsEnabledOrNot() method to check permission is already enable or not :-

public boolean CheckingPermissionIsEnabledOrNot() {

    int FirstPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA);
    int SecondPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
    int ThirdPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), SEND_SMS);
    int ForthPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), GET_ACCOUNTS);

    return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
            SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
            ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
            ForthPermissionResult == PackageManager.PERMISSION_GRANTED ;
}

 8. Set setOnClickListener() event on button :-

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    }
});

 9. Calling both RequestMultiplePermission() and CheckingPermissionIsEnabledOrNot() inside button click method in IF Else condition :-

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        // Adding if condition inside button.

        // If All permission is enabled successfully then this block will execute.
        if(CheckingPermissionIsEnabledOrNot())
        {
            Toast.makeText(MainActivity.this, "All Permissions Granted Successfully", Toast.LENGTH_LONG).show();
        }

        // If, If permission is not enabled then else condition will execute.
        else {

            //Calling method to enable permission.
            RequestMultiplePermission();

        }


    }
});

Final All Source Code of All Files :-

Code for MainActivity.java file.

package com.androidjson.multipleruntimepermission_androidjsoncom;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

// Import permission  files.
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.GET_ACCOUNTS;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.SEND_SMS;

public class MainActivity extends AppCompatActivity {

    Button button;
    public static final int RequestPermissionCode = 7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Assigning ID to button.
        button = (Button)findViewById(R.id.button1);

        // Adding Click listener to Button.
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Adding if condition inside button.

                // If All permission is enabled successfully then this block will execute.
                if(CheckingPermissionIsEnabledOrNot())
                {
                    Toast.makeText(MainActivity.this, "All Permissions Granted Successfully", Toast.LENGTH_LONG).show();
                }

                // If, If permission is not enabled then else condition will execute.
                else {

                    //Calling method to enable permission.
                    RequestMultiplePermission();

                }


            }
        });

    }

    //Permission function starts from here
    private void RequestMultiplePermission() {

        // Creating String Array with Permissions.
        ActivityCompat.requestPermissions(MainActivity.this, new String[]
                {
                        CAMERA,
                        RECORD_AUDIO,
                        SEND_SMS,
                        GET_ACCOUNTS
                }, RequestPermissionCode);

    }

    // Calling override method.
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {

            case RequestPermissionCode:

                if (grantResults.length > 0) {

                    boolean CameraPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                    boolean RecordAudioPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                    boolean SendSMSPermission = grantResults[2] == PackageManager.PERMISSION_GRANTED;
                    boolean GetAccountsPermission = grantResults[3] == PackageManager.PERMISSION_GRANTED;

                    if (CameraPermission && RecordAudioPermission && SendSMSPermission && GetAccountsPermission) {

                        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(MainActivity.this,"Permission Denied",Toast.LENGTH_LONG).show();

                    }
                }

                break;
        }
    }

    // Checking permission is enabled or not using function starts from here.
    public boolean CheckingPermissionIsEnabledOrNot() {

        int FirstPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA);
        int SecondPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
        int ThirdPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), SEND_SMS);
        int ForthPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), GET_ACCOUNTS);

        return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
                SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
                ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
                ForthPermissionResult == PackageManager.PERMISSION_GRANTED ;
    }

}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context="com.androidjson.multipleruntimepermission_androidjsoncom.MainActivity">


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Click Here to Enable Multiple Runtime Permission" />

</RelativeLayout>

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidjson.multipleruntimepermission_androidjsoncom" >

    <uses-permission android:name="android.permission.CAMERA"/>

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

    <uses-permission android:name="android.permission.SEND_SMS"/>

    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Screenshots:

Android Request Multiple Runtime Permissions at Single Request in Marshmallow

Download Code

Leave a Reply

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