Skip to content

Sample Android application to show how to get Multiple Permissions in one Go in Android M

Notifications You must be signed in to change notification settings

jaisonfdo/PermissionExample

Repository files navigation

This is a sample Android application to show how to get Multiple Permissions in one Go in Android M

Basically two files are needed to get permissions. One file is used to check and request the needed permission and the other one to send the callback.

PermissionUtils.java

Used to check and request the needed permission from the user.

PermissionResultCallback.java

It's an interface,which gives the status of the request.

interface PermissionResultCallback
{
    void PermissionGranted(int request_code);
    void PartialPermissionGranted(int request_code, ArrayList granted_permissions);
    void PermissionDenied(int request_code);
    void NeverAskAgain(int request_code);
}

Getting permissions from the user is simple,First add all the needed permissions in a list

 ArrayList permissions=new ArrayList<>();

 permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
 permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
 

Create an instance for the class "PermissionUtils", and call the function check_permission by passing the permission list,why we need those permission explanation and the request code.

 permissionUtils=new PermissionUtils(getApplicationContext());
 permissionUtils.check_permission(permissions,"Explain here why the app needs permissions",1);
 

Then redirect the onRequestPermissionsResult to permissionUtils.onRequestPermissionsResult

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        // redirects to utils

        permissionUtils.onRequestPermissionsResult(requestCode,permissions,grantResults);

    }

For getting the callbacks you need to include PermissionResultCallback interface into your activity, and also implement the needed methods.

public class PermissionActivity extends AppCompatActivity implements
        OnRequestPermissionsResultCallback,PermissionResultCallback


 // Callback functions


    @Override
    public void PermissionGranted(int request_code) {
        Log.i("PERMISSION","GRANTED");
    }

    @Override
    public void PartialPermissionGranted(int request_code, ArrayList granted_permissions) {
        Log.i("PERMISSION PARTIALLY","GRANTED");
    }

    @Override
    public void PermissionDenied(int request_code) {
        Log.i("PERMISSION","DENIED");
    }

    @Override
    public void NeverAskAgain(int request_code) {
        Log.i("PERMISSION","NEVER ASK AGAIN");
    }
    

In AndroidManifest.xml

Specify all the needed permissions. If you don't, permission requests fail silently. That's an Android thing.

For more information, check out my detailed guide here : http://droidmentor.com/multiple-permissions-in-one-go

About

Sample Android application to show how to get Multiple Permissions in one Go in Android M

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages