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_GRANTED
为true
,则不会调用。这是奇怪的,因为grantResult应该等于NOT_GRANTED
..
我可以使用targetSdkVersion 22
获取运行时权限,但我无法正确处理结果..