Skip to content

Instantly share code, notes, and snippets.

@aakashns
Created November 27, 2016 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aakashns/3f7c5317d79fc11e3e303518537a1984 to your computer and use it in GitHub Desktop.
Save aakashns/3f7c5317d79fc11e3e303518537a1984 to your computer and use it in GitHub Desktop.
Utility class to assist with requesting permissions at run time on Android.
import android.content.pm.PackageManager;
import android.os.Build;
/**
* {@link PermissionHandler} provides a simple API to request Android permissions
* at runtime. The class exposes one static method: {@link #request} which the
* requesting activity should call to request or check for permission. The requesting
* activity must implement {@link PermissionActivity}.
*/
public class PermissionHandler {
private static int[] grantedResult = new int[] { PackageManager.PERMISSION_GRANTED };
/**
* Request or check for a permission. The result of this should be handled in
* onRequestPermissionResult, which must be implemented in the calling activity.
* There are 3 cases here:
* 1. If the permission is already granted, onRequestPermissionResult is called
* with {@link #grantedResult}
* 2. If the permission is not already granted, and no rationale needs to be shown,
* the permission is requested by presenting the system dialog to the user,
* and onRequestPermissionResult is called with the result based on the selection of
* the user.
* 3. If the permission is not already granted, and an explanation (rationale) needs to
* be shown, showRationale is called. Use it to show some explanation to the user (
* e.g. using a dialog or a snackbar), and possibly request the permission again using
* {@link #request}.
* @param mActivity The activity requesting the permission. It should implement
* the interface {@link PermissionActivity}
* @param permission The permission to be requested
* @param requestCode A unique request code to be used later for reference
*/
public static void request(PermissionActivity mActivity, String permission, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
mActivity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
if (mActivity.shouldShowRequestPermissionRationale(permission)) {
mActivity.showRationale(permission, requestCode);
} else {
mActivity.requestPermissions(new String[] { permission }, requestCode);
}
} else {
mActivity.onRequestPermissionsResult(
requestCode,
new String[] { permission },
grantedResult);
}
}
/**
* Interface to be implemented by the calling activity. Only {@link #onRequestPermissionsResult}
* and {@link #showRationale} actually need to be implemented. The other methods are already
* implemented in {@link android.app.Activity}
*/
public interface PermissionActivity {
/** No need to implement this. Already implemented in {@link android.app.Activity}. */
int checkSelfPermission(String permission);
/** No need to implement this. Already implemented in {@link android.app.Activity}. */
void requestPermissions(String[] permissions, int requestCode);
/** No need to implement this. Already implemented in {@link android.app.Activity}. */
boolean shouldShowRequestPermissionRationale(String permission);
/** Implement this in your activity to handle the result of the {@link #request} **/
void onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults);
/**
* Implement this to show a rationale to the user for requesting a particular permission.
* For instance, you can show a dialog or a toast explaining why you need the permission.
* After showing the explanation, you can possibly request the permission again
* using {@link #request}
*/
void showRationale(String permission, int requestCode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment