Coder Social home page Coder Social logo

morristech / permission-requester Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tankery/permission-requester

0.0 1.0 0.0 102 KB

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

License: Apache License 2.0

Java 100.00%

permission-requester's Introduction

Permission Requester

δΈ­ζ–‡

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.0.0'
}

Gradle 3.0

dependencies {
    implementation 'me.tankery.lib:permission-requester:1.0.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)
 */
@Override
protected void 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);
}

Contribution

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

permission-requester's People

Contributors

tankery avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.