TYPE ON THE FIELD BELOW AND PRESS ENTER TO SEARCH FOR BLOG POSTS

Android Easy Runtime Permissions with Dexter Library

  • Mobile Development
Sat Apr 20 by Dinesh kumar

The purpose of app permission is to protect the privacy of the user. Android apps must request permission to access sensitive user data (such as contacts and SMS), as well as certain system features (such as camera and internet). Depending on the feature, the system might grant the permission automatically or might prompt the user to approve the request.

Dexter is an Android library that simplifies the process of requesting permissions at runtime. Dexter frees your permission code from your activities, and lets you write that logic anywhere you want.

Gradle

Firstly, add this to your Gradle dependencies in the App Level Gradle File, then Sync Project with Gradle.

dependencies { //App Permission implementation 'com.karumi:dexter:5.0.0' }
Step 1 : Activity

To start using the library, you just need to call Dexter with a valid Activity.

public MyActivity extends Activity { @Override public void onCreate() { super.onCreate(); Dexter.withActivity(activity) .withPermission(permission) .withListener(listener) .check(); } }
Step 2 : Single Permission

For each permission, register a PermissionListener implementation to receive the state of the request.

Dexter.withActivity(this) .withPermission(Manifest.permission.CAMERA) .withListener(new PermissionListener() { @Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */} @Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */} @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */} }).check();
Step 3 : Single Permission Listener
  1. BasePermissionListener to make it easier to implement only the methods you want. Keep in mind that you should not call super methods when overriding them.
  2. DialogOnDeniedPermissionListener to show a configurable dialog whenever the user rejects a permission request.
  3. SnackbarOnDeniedPermissionListener to show a snackbar message whenever the user rejects a permission request.
  4. CompositePermissionListener to compound multiple listeners into one.
PermissionListener dialogPermissionListener = /*...*/; PermissionListener snackbarPermissionListener = /*...*/; PermissionListener compositePermissionListener = new CompositePermissionListener(snackbarPermissionListener,dialogPermissionListener, /*...*/);
Step 4 : Multiple Permission

To request multiple permissions, you just need to call withPermissions and register an implementation of MultiplePermissionsListener. The MultiplePermissionsReport contains all the details of the permission request like the list of denied/granted permissions or utility methods like areAllPermissionsGranted and isAnyPermissionPermanentlyDenied.

Dexter.withActivity(this) .withPermissions( Manifest.permission.CAMERA, Manifest.permission.READ_CONTACTS, Manifest.permission.RECORD_AUDIO ).withListener(new MultiplePermissionsListener() { @Override public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */} @Override public void onPermissionRationaleShouldBeShown(List permissions, PermissionToken token) {/* ... */} }).check();
Step 5 : Multiple Permission Listener
  1. BaseMultiplePermissionsListener to make it easier to implement only the methods you want. Keep in mind that you should not call super methods when overriding them.
  2. DialogOnAnyDeniedMultiplePermissionsListener to show a configurable dialog whenever the user rejects at least one permission.
  3. SnackbarOnAnyDeniedMultiplePermissionsListener to show a snackbar message whenever the user rejects any of the requested permissions.
  4. CompositePermissionListener to compound multiple listeners into one.
MultiplePermissionsListener snackbarMultiplePermissionsListener = /*...*/; MultiplePermissionsListener dialogMultiplePermissionsListener = /*...*/; MultiplePermissionsListener compositePermissionsListener = new CompositeMultiplePermissionsListener( snackbarMultiplePermissionsListener, dialogMultiplePermissionsListener, /*...*/);
Step 6 : Handling Listener Threads

If you want to receive permission listener callbacks on the same thread that fired the permission request, you just need to call onSameThread before checking for permissions.

Dexter.withActivity(activity) .withPermission(permission) .withListener(listener) .onSameThread() .check();
Step 6 : Error Handling

If you think there is an error in your Dexter integration, just register a PermissionRequestErrorListener when calling Dexter. The library will notify you when something bad happens. In general, it is a good practice to log every error Dexter may throw, but is up to you, the developer, to do that.

Dexter.withActivity(activity) .withPermission(permission) .withListener(listener) .withErrorListener(new PermissionRequestErrorListener() { @Override public void onError(DexterError error) { Log.e("Dexter", "There was an error: " + error.toString()); } }).check();

Dexter frees your permission code from your activities and lets you write that logic anywhere you want. Using this library, the permissions can be implemented in a few minutes. Do let us know your thoughts!

FOUND THIS USEFUL? SHARE IT

Ready to Build Your Projects?

The best companies and startups hire our top dev teams, and so can you.
Start a Project
GSM Plus Infotech