如何检查单击运行时权限中的“允许”选项

时间:2018-06-13 12:11:35

标签: java android runtime-permissions

在我的应用中,我想对runtime permissionsStorage使用Camera

为此,我写了下面的代码,它适用于我,但我想知道用户是否点击了“允许”选项。

private static final int CAMERA_REQ = 2008;
private static final int STORAGE_REQ = 2005;
@OnClick(R.id.newAddFrag_uploadImageRootLay)
void uploadImage() {
    new MaterialDialog.Builder(getActivity())
            .title("Select")
            .items(R.array.uploadImages)
            .titleGravity(GravityEnum.CENTER)
            .itemsGravity(GravityEnum.CENTER)
            .typeface("IranSans.ttf", "IranSans.ttf")
            .itemsIds(R.array.itemIds)
            .itemsCallback(new MaterialDialog.ListCallback() {
                @Override
                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                    switch (which) {
                        case 0:
                            if (checkIfStorage(context)) {
                                Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                startActivityForResult(galleryIntent, REQUEST_PICK_PHOTO);
                            } else {
                                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_REQ);
                            }
                            break;
                        case 1:
                            if (checkIfCamera(context)) {
                                captureImage();
                            } else {
                                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, CAMERA_REQ);
                            }
                            break;
                    }
                }
            })
            .show();
}

private boolean checkIfCamera(Context context) {
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}

private boolean checkIfStorage(Context context) {
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 0) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            newAddFrag_uploadImageRootLay.setEnabled(true);
            newAddFrag_uploadImageSuccessTxt.setEnabled(true);
            newAddFrag_uploadImageLay.setEnabled(true);
        }
    }
    if (requestCode == CAMERA_REQ) {
        Log.e("permissionsLog", "CAMERA");
    }
    if (requestCode == STORAGE_REQ) {
        Log.e("permissionsLog", "STORAGE");
    }
}

2 个答案:

答案 0 :(得分:0)

使用支持库时,必须使用正确的方法调用。例如:

  • AppCompatActivity 中,您应该使用ActivityCompat.requestPermissions;

  • android.support.v4.app.Fragment 中,您应该只使用requestPermissions(这是android.support.v4.app.Fragment的实例方法)

如果您在片段中调用 ActivityCompat.requestPermissions ,则会在活动而不是片段上调用 onRequestPermissionsResult 回调。

希望解释有帮助!

要获得更多知识,请阅读here

答案 1 :(得分:0)

请求许可时隐含以下规则:

  • 如果您在android.support.v4.app.Fragment中请求权限,则只需致电requestPermissions(因为它已在支持lib中提供)。
  • 如果您在片段中致电ActivityCompat.requestPermissions,那么onRequestPermissionsResult回调将会传递给活动而非Fragment

由于您要在android.support.v4.app.Fragment中请求权限,只需在requestPermissions中使用onRequestPermissionsResult或覆盖Activity并在那里处理结果。