Code Monkey home page Code Monkey logo

permissionhelper's Introduction

PermissionHelper

PermissionHelper.java is a file which will let you handle Android 6.0 permissions system easily.

To start, copy the file PermissionHelper.java into your project.

To request a permission in an Activity, first override the onRequestPermissionsResult and call PermissionHelper.onRequestPermissionsResult inside:

@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionHelper.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}

Then, you can require all the permissions you need by simply calling: PermissionHelper.request([this activity], [permission needed], [permission callback]) this way:

PermissionHelper.request(MainActivity.this, Manifest.permission.CAMERA, new PermissionHelper.PermissionListener() {
    @Override void onPermitted(@NonNull String permission) {
        // execute actions
    }
});

That's all!

Optionally, you can execute some actions when the permission is forbidden (like closing the Activity or disabling buttons). This can be done by extending the onForbidden method of the PermissionListener:

PermissionHelper.request(MainActivity.this, Manifest.permission.CAMERA, new PermissionHelper.PermissionListener() {
    @Override void onPermitted(@NonNull String permission) {
        // execute actions
    }
    
    @Override void onForbidden(@NonNull String permission) {
        super.onForbidden(permission);
        finish();
    }
});

Optionally, as stated by Requesting Permissions at Run Time page in official guidelines, you can show messages that explain why the permission should be accepted. To do this, you have to extend shouldTryToMotivate and make it returns true, then extend motivationMessage and denialMessage this way:

PermissionHelper.request(MainActivity.this, Manifest.permission.CAMERA, new PermissionHelper.PermissionListener() {
    @Override void onPermitted(@NonNull String permission) {
        // execute actions
    }
    
    @Override boolean shouldTryToMotivate() {
        return true;
    }

    @Override int motivationMessage() {
        return R.string.motivation_flashlight;
    }

    @Override int denialMessage() {
        return R.string.denial_flashlight;
    }
});

motivationMessage() is a message which tells the user that he have to grant the permission by pressing OK in the dialog which appears. The permission will be requested another time and he will have another chance to accept it. denialMessage() is the message which appears when the user totally denied the granting of the permission and it can't be requested anymore.

You have to use string resources, so that you can easily translate those strings in other languages.

Note that when you use this motivational system, onForbidden will be called only when the user totally refuses to accept permission and to see other permission request of the same kind. Which means, it gets called only when the denialMessage is shown.

permissionhelper's People

Contributors

mikewellback avatar

Stargazers

Antonello Galipò avatar

Watchers

James Cloos avatar  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.