Dexter is an Android library to simplify the process of requesting permissions at runtime, where they can accept or reject permission while using an application.
You have seen many applications in which they ask for certain permission to allow or deny according to your choice. But an application makes the features work better for you when you give them access to certain details or other things.
Android Marshmallow offers a new function (Dexter) that allows users to accept or reject permission while using the application, instead of accepting them all during the app installation.
In this blog, we are going to simplify the process of adding the runtime permissions using the Dexter library. Using this library, the permissions can be implemented in few minutes.
To get started with Dexter, Include the library in your build.gradle
To request single permission, you can use withPermission() method by passing the required permission. You also need a PermissionListener callback to receive the state of the permission.
onPermissionGranted() will be called once the permission is granted.
onPermissionDenied() will be called when the permission is denied. Here you can check whether the permission is permanently denied by using response.isPermanentlyDenied() condition.
The below code requests READ_SMS permission.
To request multiple permissions at the same time, you can use withPermissions() method. Below code requests READ & WRITE STORAGE and READ & WRITE CONTACTS permissions.
You can also catch any errors occurred while integrating the library using PermissionRequestErrorListener.
Now Let’s See How To Use Dexter In An Example Project.
Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates.
Include the library in your build.gradle
Add following permissions in AndroidManifest.xml
Open the layout file of your MainActivity.java (activity_main.xml) and add two buttons to test single permission & multiple permission methods.
Open MainActivity.java and update it
This is an introductory article about the Dexter covering basic features offered by the library. Dexter provides other features like using it with SnackBar, different types of listeners, error handling and few other. You can find more information on Dexter’s developer page.
This blog was wrapped around the process of adding runtime permissions using the Dexter library. It will be helpful to learn and practice it, to develop the client's application with and use this function to give them the best-featured app.
CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!