Skip to content

Simple class for give multiple permission with explorations for android devices.

License

Notifications You must be signed in to change notification settings

dec04/Android-permission-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Android permission utils

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


Just add permissionUtils.java to your project.

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. 💟

About

Simple class for give multiple permission with explorations for android devices.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages