Simplest way to Request Multiple Permissions in Android Applications


All Android applications running android 6.0 and above requires that permissions be requested during application’s runtime. Today we are going to see one of the simplest methods to request android runtime permissions using Easy Permissions Library. This walkthrough will be simple than you would actually imagine.

Understanding how to request Android Permissions

Android permissions are required only when permission operation is required, for example, you need to check if app has storage permissions, everytime you perform storage operation like reading files or writing to external storage. Easy Permissions has some strong inbuilt methods to help you check and request for permissions with a few lines of code.

How TO REQUEST MULTIPLE PERMISSIONS IN ANDROID APPLICATIONS USING EASY PERMISSIONS Library

Go ahead and create a new application in the android studio. Add this implementation in your build.gradle file.

DOWNLOAD PROJECT | Download Application

dependencies {
    implementation 'pub.devrel:easypermissions:1.2.0'
}

Add necessary permissions in your manifest file as shown in the code below.



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

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

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

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

in your MainACtivity.java file, add the following code, am going to explain everything after this code

package ke.co.potent_mshop.myapplication;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

    private static final int REQUEST_CODE = 121;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        if (Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
            String[] perms = {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_CONTACTS,
                    Manifest.permission.WRITE_CONTACTS
            };


            if (!EasyPermissions.hasPermissions(this, perms)) {
                EasyPermissions.requestPermissions(this, "All permissions are required in oder to run this application", REQUEST_CODE, perms);
            }


        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.requestPermissions(this, "All permissions are required to run this application", requestCode, permissions);

    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        // if permissions have been granted, or has been passed, take an action here,
        // perform your action here
        // this method will be called as soon as permissions have been granted
        // you can add a toast message here, to see what happens
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            // if some permissions has been denied, show a settings dialog
            // that will take user to the application permissions setting secreen
            new AppSettingsDialog.Builder(this).build().show();
        }

    }
}



Understanding how to implement EASY PERMISIONS in android studio

First, you will need to implement EasyPermissions.PermissionCallbacks on your activity or fragment, then override necessary methods as shown in the example below, only two methods will be overridden, that is, onPermissionsDenied and onPermissionsGranted. You also need to manually override another method onRequestPermissionsResult.

onPermissionsDenied will be called as soon as permissions have been denied by the user, it’s in this method where we will check if some permissions have been permanently denied.

onPermissionsGranted will be called as soon as user grants all the permissions or some permissions.

onRequestPermissionsResult will be used to receive the action of requesting permissions, just as we have on activityResult which get called everytime an action is required.

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
   //...

   @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.requestPermissions(this, "All permissions are required to run this application", requestCode, permissions);

    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        // if permissions have been granted, or has been passed, take an action here,
        // perform your action here
        // this method will be called as soon as permissions have been granted
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            // if some permissions has been denied, show a settings dialog
            // that will take user to the application permissions setting secreen
            new AppSettingsDialog.Builder(this).build().show();
        }

    }

        
}

Now we let’s learn how to check if permissions exist. To check if permissions exist or not, Easy Permissions library has an inbuilt method EasyPermissions.hasPermissions(this, perms). Now basically here is how to go about it

if (!EasyPermissions.hasPermissions(this, perms)) {
                EasyPermissions.requestPermissions(this, "All permissions are required in oder to run this application", REQUEST_CODE, perms);
            }

Remember to check if users are using android 6 and above or Android 5 and below. Remember permissions check are not required for android 5 and below, have a look at the code below.

if (Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
            String[] perms = {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_CONTACTS,
                    Manifest.permission.WRITE_CONTACTS
            };


            if (!EasyPermissions.hasPermissions(this, perms)) {
                EasyPermissions.requestPermissions(this, "All permissions are required in oder to run this application", REQUEST_CODE, perms);
            }


        }


As soon as you check those permissions, actions will be received in relevant overridden methods of the Easy Permissions library. And that’s how to request permissions in android using Easy Permissions library.

VIDEO ON HOW TO REQUEST MULTIPLE PERMISSIONS IN ANDROID USING EASY PERMISSIONS LIBRARY