Skip to content

A simple permission request activity that let you grant permissions easylly.

License

Notifications You must be signed in to change notification settings

tankery/permission-requester

Repository files navigation

Permission Requester

Build Status GitHub release Bintray

中文

Permission Reuquester is a simple permission request activity that let you grant permissions easylly, with a standard policy, that covers all of situations relative.

Background

Requesting permissions at run time is a new feature added from Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. An App target at 23 or above, need to requesting the "Dangerous" permission at run time.

There is lot of things to do.

You need to Check For Permissions to see if the permissions are granted. If not, you need to Request the permissions you need, and you may need to Explain why the app needs permissions. Somethings you also need to Handle the permissions request response to do things after permission granted. What's more, if user selected "Don't ask again", you will not have a chance to requesting permissions from App, then you may need to guide user to settings to open the permissions.

Workflow

Here is a workflow for permission request policy:

When you start requesting permission with PermissionRequestActivity, it will first check if we already have permission. If don't, show system permission requesting dialogs to ask for permissions. Then show a "rationale dialog" for user if he denied the permissions, or show a "go settings dialog" to guide user to settings when he click "Don't ask again".

Download

Gradle

dependencies {
    compile 'me.tankery.lib:permission-requester:1.1.0'
}

Gradle 3.0

dependencies {
    implementation 'me.tankery.lib:permission-requester:1.1.0'
}

Usage

If you don't need response when requesting finished, all you need to do is to start the PermissionRequestActivity using:

PermissionRequestActivity.start(context, PERMISSIONS, rationalMsg, goSettingsMsg);

You can even start this activity from background Service.

If you care about the requesting response, start PermissionRequestActivity from your activity:

PermissionRequestActivity.start(activity, REQUEST_CODE, PERMISSIONS, rationalMsg, goSettingsMsg);

Then, handle the activity result in onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Permission granted
        } else {
            // Permission denied
        }
    }
}

You can also override showRationaleDialog to show a custom rationale dialog when need:

/**
 * Override this method to show custom dialog.
 * @param canRequestAgain if true, show request again dialog, else, show go settings dialog
 * @param message dialog message
 * @param dialogResult always have a result for user action
 *                     (ok - > positive/cancel -> negative/dismiss -> negative)
 * @return The custom dialog shown.
 */
@Override
protected Dialog showRationaleDialog(final boolean canRequestAgain, String message,
                                     final @NonNull DialogResult dialogResult) {
    AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setMessage(message)
            .setCancelable(true)
            .setPositiveButton(android.R.string.ok, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                    dialogResult.onPositive();
                }
            })
            .setNegativeButton(android.R.string.cancel, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            })
            .setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    dialogInterface.dismiss();
                    dialogResult.onNegative();
                }
            })
            .show();
    alertDialog.setCanceledOnTouchOutside(true);
    return alertDialog;
}

Contribution

Feel free to create issues when you find some bug or have some suggestions. And Pull Request is welcome. Thanks for your attention~

About

A simple permission request activity that let you grant permissions easylly.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages