我正试图通过单击底页上的textview
来请求多个权限。请求部分起作用。但是,onRequestPermissionsResult
似乎无效。
这是代码。
public class BottomSheetProfile extends BottomSheetDialogFragment {
private ProfileBottomSheetListener bottomSheetListener;
private static final int REQUEST_PERMISSIONS_ALL = 1;
private String[] allPermissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
public static BottomSheetProfile newInstance() {
return new BottomSheetProfile();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View bottomSheetProfile = inflater.inflate(R.layout.bottom_sheet_profile, container, false);
TextView tvActionCamera = bottomSheetProfile.findViewById(R.id.tv_action_camera);
TextView tvActionGallery = bottomSheetProfile.findViewById(R.id.tv_action_gallery);
tvActionCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchCamera();
dismiss();
}
});
return bottomSheetProfile;
}
private void launchCamera() {
if (hasAllPermissions(getActivity(), allPermissions)) {
Toast.makeText(getActivity(), "All permissions granted", Toast.LENGTH_SHORT).show();
} else {
requestPermissions(allPermissions, REQUEST_PERMISSIONS_ALL);
}
}
private boolean hasAllPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSIONS_ALL) {
boolean allPermissions = true;
if (grantResults.length > 0) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
allPermissions = false;
}
if (allPermissions) {
Toast.makeText(getActivity(), "All Permissions granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Request again", Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
希望我很清楚