googlesamples / easypermissions

Simplify Android M system permissions
https://firebaseopensource.com/projects/googlesamples/easypermissions/
Apache License 2.0
9.86k stars 1.46k forks source link

permissionsgranted and permissionsdenied will be called continuously. #292

Closed centerzx closed 4 years ago

centerzx commented 4 years ago

Basic Information

Device type: _xiao mi 4S___ OS version: _7.0___ EasyPermissions version: _2.0.1___

DES:

Request permission: manifest.permission.camera, manifest.permission.record'audio, manifest.permission.write'external'storage, manifest.permission.read'external'storage

Among them, 2 are allowed, 1 is denied, and the operation method is added to the onpermissionsgranted method, and then click the button to request permission. At this time, permissionsgranted and permissionsdenied will be called continuously.

What happened? What did you expect to happen?

Code and logs

public void grantedStartLive() {
        String[] perms = new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(getActivity(), perms)) {
            startLive();
        } else {
EasyPermissions.requestPermissions(getActivity(),App.getApplication().getString(R.string.permission_live_req_tips), PermissionUtil.LIVE_PERMISSION_CODE, perms);
        }
    }

@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PermissionUtil.LIVE_PERMISSION_CODE) {
            grantedStartLive();
        }
    }

@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            if (requestCode == PermissionUtil.LIVE_PERMISSION_CODE) {
                showLivePermissionDialog();
            } 
        }
    }

...........THX

bitvale commented 4 years ago

Try to add perms size check in onPermissionsGranted:

if (requestCode == PermissionUtil.LIVE_PERMISSION_CODE) {
        if (perms.size() == 4) {
               grantedStartLive(); 
        }
}
samtstern commented 4 years ago

Sorry I never responded here! I totally missed this. @bitvale has the correct solution though, onPermissionsGranted will be called even if only some permissions are granted.