Source: pixabay.com

Simple permission Helper for Android.

Harshith Shetty
3 min readJan 30, 2018

Well if you’re new to Android, you’ll come across Android Marshmallow (M) run-time permissions and then you’ll have to implement handler for it.

Main checklist for this is:

  • Check if the phone is having version less than Android Marshmallow (API level 23), if yes then you’re good to go and skip the rest, if not then following steps apply
  • Check if you already have the permissions you wish for by checking,
ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED)
  • If this gives false, then your app doesn’t have permissions to access it so you have to request for permissions,
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
  • And after that you have to handle the result in the callback,
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// permission was granted, yay! Do the
// contacts-related task you need to do.

} else {

// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}

// other 'case' lines to check for other
// permissions this app might request.
}
}
  • And we also have to check if we have to show user some info as why we are asking for the permissions,
ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)
  • If we are asking permission for the first time, this gives false as ask the user directly, after we asked once and user denies then this function returns true, but if the user selects deny and don’t ask again then this gives false in onRequestPermissionsResult then we have to ask him to turn it on from settings, and navigate user to the app settings

So, handling permissions is a tiresome task if we have to do all the steps for each and every time we need permissions so bored of it, I made a simple solution,

PermissionHelper

This is a small utility library I have made to simplify this task, it handles permission handling for you and give you 2 simple callbacks for success or fail and includes a alert for asking permission again and even asking user to turn on permissions from settings if they have checked “never ask again”.

Its implementation is just,

  • Declare
PermissionHelper permissionHelper;
  • Initialize with the current Activity/Fragment and set the listener
permissionHelper=new PermissionHelper(this).setListener(new PermissionHelper.PermissionsListener() {
@Override
public void onPermissionGranted(int request_code) {

//if user grants permission.

}

@Override
public void onPermissionRejectedManyTimes(List<String> rejectedPerms,int request_code) {

//if user keeps on denying request
}
});
  • Request permission(s)
String[] needed_permissions=new String[]{Manifest.permission.CAMERA, Manifest.permission.CALL_PHONE};permissionHelper.requestPermission(needed_permissions,100);
  • Add a hook on callback and you’re done.
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
/**
* pass the permission results to the PermissionHelper for processing.
*/
permissionHelper.onRequestPermissionsResult(requestCode,permissions,grantResults);}
  • It will ask the user for the permissions you need and if he denies, will ask him to accept as the app needs it and based on that will give you callback.

A minimal library to simplify and let you focus on task and handles the permission itself.

References:

https://developer.android.com/training/permissions/requesting.html
https://github.com/har5hit/PermissionHelper

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Harshith Shetty
Harshith Shetty

Written by Harshith Shetty

Mobile App Architect @Shaadi.com

No responses yet

Write a response