Welcome folks I am back with another blog post. In this post we will be implementing Android Marshmallow Runtime Permissions using an Android library called as Dexter. Dexter allows you to to implement these runtime permissions with ease. Whenever user wants to initiate some action it will ask the user the appropriate permission at runtime. Let’s implement this and get started with the post.

This is a introductory post to the library Dexter. I would like to tell this library handles lots of other features as well including snackbar, error handling. You can read more about it in the Dexter Developers Page.

To get started with the library you need to include it in the build.gradle file. Just add the following lines of code given below.

REQUESTING SINGLE PERMISSION USING DEXTER

Now to request Single Permission using the Dexter Library you need to use withPermission() and also this method takes the required permission to pass. We will also need a callback listener in the form of PermissionCallback to get the state of the permission. This callback has two methods namely which are as follows:                                                                                                                                 1) onPermissionGranted() -> It will be called whenever the permission has been granted by the user.

2) onPermissionDenied() -> This method will be called whenever the permission has been denied by the user.

If the user has permanently denied the permission then you can also check this by using the response.isPermanentlyDenied() condition. 

The below code is given for the camera permission. So after executing this code user will granted with the camera permission. First of all just make sure you include the camera permission in the manifest file like this:

After including the permission in the manifest file. Just copy paste this code in layout file.

After making the layout of this permission. We need to write Java code to implement the runtime permission using Dexter. Just copy paste this code in activity_main.java file.

As you can see when the app launches and the user clicks the camera button it first of all asks the user dynamically at runtime that it requires the camera permission before performing the actual operation. If I allow the permission then only the camera application opened. But in the scenario if I deny the permission permanently then it will close the application. This is my implementation in the scenario when the user rejects the permission.

REQUESTING MULTIPLE PERMISSIONS USING DEXTER

Similarly we can also request multiple permissions using Dexter. For this purpose the process will remain the same. First of all include the necessary permissions in the manifest file. In this case I am taking read_external_storage, write_internal_storge and Internet Permission.

Similarly we can also request multiple permissions using Dexter. For this purpose the process will remain the same. First of all include the necessary permissions in the manifest file. In this case I am taking read_external_storage, write_internal_storge and Internet Permission.

After including the permission in the manifest file. Just copy paste this code in layout file.

After making the layout of the permission we will making the logic in the java file. So just copy paste the code given below.

Congratulations we are done implementing runtime permissions using Dexter. Thanks for reading the post and if you want to receive more tutorials like this please subscribe to the newsletter.

A programmer scientist by the mind and a passionate blogger by heart ❤️. Fountainhead of Coding Shiksha Life motto: Live while you can! Teach & inspire while you could & Smile while you have the teeth.

Gautam Sharma

Founder , Coding Shiksha

0 Comments

Submit a Comment

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