因为你在许可证上循环。
你的方法应该是这样的。
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case Constants.CAPTURE_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
takePicture();
} else {
//camera permission denied
show an explaining message to tell the user why you need that permission
}
return;
}
}
}
您也可以这样做(更完整的方法)
private boolean checkCameraPicturePermissions() {
canAccessCamera = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
canReadExternal = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
canWriteExternal = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
List<String> permissionsNeeded = new ArrayList<>();
if(!canAccessCamera) {
permissionsNeeded.add(Manifest.permission.CAMERA);
}
if(!canReadExternal) {
permissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if(!canWriteExternal) {
permissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionsNeeded.isEmpty()) {
requestPermissions(permissionsNeeded.toArray(new String[permissionsNeeded.size()]), PERMISSION_REQUEST_CAMERA_PICTURE);
return false;
}
return true;
}
btnCameraAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkCameraPicturePermissions()) {
onTakePhoto();
}
}
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CAMERA_PICTURE: {
Map<String, Boolean> permissionsMap = new HashMap<>();
permissionsMap.put(Manifest.permission.CAMERA, true);
permissionsMap.put(Manifest.permission.READ_EXTERNAL_STORAGE, true);
permissionsMap.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, true);
if (grantResults.length > 0) {
for (int i = 0; i < grantResults.length; i++) {
permissionsMap.put(permissions[i], grantResults[i] == PackageManager.PERMISSION_GRANTED);
}
canAccessCamera = permissionsMap.get(Manifest.permission.CAMERA);
if (permissionsMap.get(Manifest.permission.CAMERA)
&& permissionsMap.get(Manifest.permission.READ_EXTERNAL_STORAGE)
&& permissionsMap.get(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)
|| shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)
|| shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
rationale(getString(R.string.warning_rationale_permissions));
} else {
explain(getString(R.string.mandatory_permissions));
}
}
}
break;
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void rationale(String value) {
try {
InformationPopup informationPopup = InformationPopup.newInstance(getString(R.string.warning_title), value, getString(R.string.action_ok), null);
informationPopup.mListener = new InformationPopup.InformationActionHandler() {
@Override
public void onButtonLeft(InformationPopup dialog) {
dialog.dismiss();
}
};
} catch (Exception e) {}
}
private void explain(String value) {
try {
ConfirmationPopup confirmationPopup = ConfirmationPopup.newInstance(
getString(R.string.warning_title),
value,
getString(R.string.action_no),
getString(R.string.action_yes), null);
confirmationPopup.setCancelable(true);
confirmationPopup.mListener = new ConfirmationPopup.ConfirmationActionHandler() {
@Override
public void onButtonLeft(ConfirmationPopup dialog) {
dialog.dismiss();
}
@Override
public void onButtonRight(ConfirmationPopup dialog) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromParts("package", getContext().getPackageName(), null);
intent.setData(uri);
getContext().startActivity(intent);
}
@Override
public void onButtonRight(ConfirmationPopup dialog, Object object) {
}
@Override
public void onButtonRight(ConfirmationPopup dialog, int index) {
}
};
confirmationPopup.show(getFragmentManager(), "RationalePermissionPopup");
} catch (Exception e) {}
}
ConfirmationPopup和InformationPopup继承了DialogFragment,但是您可以创建一个常规的DialogFragment。希望有帮助。