Use the Dexter library to manage Runtime Permissions in Android

Tram Ho

We all know that Android Marshmallow introduced runtime permissions that allow users to allow or deny all permissions at run time.

In this article, we will simplify obtaining permission to use permissions using the Dexter library. Using this library, permissions will be granted in just a few minutes.

1. Use the Dexter library

To use the Dexter library, add dependency to the project’s app / build.gradle :

1.1 Request a Permission

To request a permission, you can use the withPermission () function by passing the required permission. You also need the PermissionListener callback to get permission status.

> onPermissionGranted () will be called after permission is granted. > onPermissionDenied () will be called when permission is denied. You can check if the permission is permanently denied or not by using the answer.isPternalDenied () condition.

The following code snippet will require using the CAMERA permission:

1.2 Request multiple Permission

To request multiple permissions at once, you can use the withPermissions () function.

The following code snippet will require using permission STORAGE and LOCATION :

1.3 Error handling occurred

2. Create a new Projetc

1. Android Studio ⇒ File ⇒ New Project ⇒ Basic Activity

  1. Add the Dexter library to the build.gradle file

  1. Open the Main Activity layout: activity_main.xml and content_main.xml

content_main.xml

  1. MainActivity.java

Share the news now

Source : Viblo