0

This is how I request permissions :

  private boolean checkAndRequestPermissions() {
        int permissionSendMessage = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
        int cameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        int phoneStatePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

        List<String> listPermissionsNeeded = new ArrayList<>();
        if (locationPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
        if (cameraPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.CAMERA);
        }

        if (phoneStatePermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
        }

        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }

I call this method after click button :

@OnClick(R.id.email_sign_in_button)
void loginButton() {
    if(checkAndRequestPermissions()) {
        attemptLogin();
    }
}

But when a user confirm all permissions have to click one more time button. What I have to do that user do not click a button ?

  • 1
    have a look this answer http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission/43322136#43322136 – Vikas Tiwari May 12 '17 at 07:36

3 Answers3

0

You need to override the activity's onRequestPermissionsResult method, it will be called when the user accepts/rejects permissions. You can find more details HERE

Here is an example specific to your case:

@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
    if(requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS && grantResults.length == listPermissionsNeeded.size()){
        boolean allGranted = true;
        for(int grantedResult : grantedResults){
            if(grantedResult != PackageManager.PERMISSION_GRANTED){
                allGranted = false;
            }
        }
        if(allGranted){
            attemptLogin();
        }
    }
}
Titus
  • 22,031
  • 1
  • 23
  • 33
0

You can listen for the onRequestPermissionsResult in your Activity/Fragment.

As statet in the Documentation, you can implement this to check if the user has granted the requested permissions:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
             // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

Remember to alter the switch/case according to your needs. If the user has granted all permissions, just call attemptLogin() once again.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
W3hri
  • 1,563
  • 2
  • 18
  • 31
0

Use RxPermissions: No need to again click on button after allow permissions

gradle

compile 'com.tbruyelle.rxpermissions:rxpermissions:0.7.0@aar'
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'

code

 RxPermissions.getInstance(this)
                        .request(REQUEST_PERMISSIONS)
                        .subscribe(new Action1<Boolean>() {
                            @Override
                            public void call(Boolean granted) {
                                if (granted) {
                                    attemptLogin();
                                }
                            }
                        });
Hitesh Gehlot
  • 1,307
  • 1
  • 15
  • 30