片段中的运行时权限返回错误的grantResult ..我正在使用`targetSdkVersion 22`

时间:2018-01-03 11:57:05

标签: java android permissions fragment

enter image description here我的代码:IN片段: -

if (((PermissionChecker.checkSelfPermission(getContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) != PackageManager.PERMISSION_GRANTED)) {
                        requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_READ);

                    }

                    else {

                        share();

                    }

并根据要求获得结果: -

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

    if (requestCode == REQUEST_PERMISSION_READ) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted.
            share();
        } else {
            btn_share.setVisibility(View.VISIBLE);
            CheckConstraint.getbuilder(getActivity(),getString(R.string.please_allow_permission));
            Toast.makeText(mContext, "Permission not granted", Toast.LENGTH_SHORT).show();

        }
    }
    else if (requestCode==REQUEST_PERMISSION_WRITE)
        if (grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)
            share();
        else {
            Toast.makeText(mContext, "Permission not granted", Toast.LENGTH_SHORT).show();

        }
        else
    {
        super.onRequestPermissionsResult(requestCode,permissions,grantResults);
    }


}

如果ALLOW,则不会调用permissionResult,如果DENY grantResult[0]==PackageManager.PERMISSION_GRANTEDtrue,则不会调用。这是奇怪的,因为grantResult应该等于NOT_GRANTED .. 我可以使用targetSdkVersion 22获取运行时权限,但我无法正确处理结果..

0 个答案:

没有答案