Coder Social home page Coder Social logo

dec04 / permissionutilslibrary Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 1.17 MB

Android library to geet multiple android permissions

Home Page: https://jitpack.io/#dec04/PermissionUtilsLibrary

License: MIT License

Java 100.00%
android-permissions permissions android android-tools android-tool android-ui

permissionutilslibrary's Introduction

Android permission utils

Simple class for give multiple permissions on android device, with simple explorations.


How to:

Gradle

Step 1. Add the JitPack repository to your build file

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.dec04:PermissionUtilsLibrary:1.0.1'
}

Basic usage:

In order for each permission to show its explanation, you need to add lines in the strings.xml file Required format for string resource names is PERMISSION_NAME + _EXPLANATION

For example if you request this permissions (ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, WRITE_EXTERNAL_STORAGE) names must be:

<string name="ACCESS_FINE_LOCATION_EXPLANATION">Some explanation</string>
<string name="ACCESS_COARSE_LOCATION_EXPLANATION">Some explanation</string>
<string name="WRITE_EXTERNAL_STORAGE_EXPLANATION">Some explanation</string>

Code from my google map example:

Context applicationContext;
PermissionUtils permissionUtils;
String[] permissions = {
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.ACCESS_COARSE_LOCATION,
    Manifest.permission.WRITE_EXTERNAL_STORAGE
};

Usually in a method OnCreate:

applicationContext = GoogleMapActivity.this;
permissionUtils = new PermissionUtils(applicationContext);

Check permissions:

// ... some code ...

// Check all permissions
if (permissionUtils.check(permissions)) {
    // if permissions already taken
    // do something
} else {
    // request permission
    permissionUtils.requestPermission(permissions);
}

If permission not taken, app request it from user. All at once.

Permission request Permission request

Usualy in OnResume or OnResumeFragment method:

if (permissionUtils.flagPermissionDenied) {
    // Permission was not granted, display "permission rejected" dialog.
    permissionUtils.showMissingPermissionError(permissions);
    permissionUtils.flagPermissionDenied = false;
}

Now, if user reject permission app show window, explaining that the permit is not received.

Permission request

The next time you request permissions, a window will be displayed explaining why you need each permission individually. In the last window, you can re-request permission.

Permission requestPermission requestPermission request

You also need implement method onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode != PERMISSION_REQUEST_CODE) {
        return;
    }

    if (permissions.length != 0) {
        if (!permissionUtils.check(permissions)) {
            // Display the missing permission error dialog when the fragments resume.
            permissionUtils.flagPermissionDenied = true;
        } else {
            // For example enable the "my location layer" if the permission has been granted.
        }
    }
}

That's all. Happy hacking. ๐Ÿ’Ÿ

p.s.

IF YOU NEED EXAMPLES;

permissionutilslibrary's People

Contributors

dec04 avatar

Stargazers

 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.