单击按钮后Java Android请求权限

单击按钮后Java Android请求权限,java,android,Java,Android,以下是我请求权限的方式: private boolean checkAndRequestPermissions() { int permissionSendMessage = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); int locationPermission = ContextCompat.checkSelfPermission(t

以下是我请求权限的方式:

  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;
    }

但当用户确认所有权限时,必须再单击一次按钮。如果用户不单击按钮,我必须做什么?

您需要覆盖活动的
onRequestPermissionsResult
方法,当用户接受/拒绝权限时将调用该方法。你可以找到更多的细节

以下是一个特定于您的案例的示例:

@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();
        }
    }
}

您可以在活动/片段中侦听onRequestPermissionsResult

作为中的statet,您可以实现此功能以检查用户是否已授予请求的权限:

@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
    }
}

记住根据需要更换开关/外壳。如果用户已授予所有权限,只需再次调用attemptLogin()。

使用RxPermissions:在允许权限后无需再次单击按钮

格拉德尔

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

RxPermissions.getInstance(此)
.请求(请求权限)
.订阅(新操作1(){
@凌驾
公共无效调用(已授予布尔值){
如果(授予){
尝试登录();
}
}
});
看看这个答案
compile 'com.tbruyelle.rxpermissions:rxpermissions:0.7.0@aar'
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
 RxPermissions.getInstance(this)
                        .request(REQUEST_PERMISSIONS)
                        .subscribe(new Action1<Boolean>() {
                            @Override
                            public void call(Boolean granted) {
                                if (granted) {
                                    attemptLogin();
                                }
                            }
                        });