在Marshmallow中,如果第一次拒绝权限,则运行时权限对话框不会再次提示。 我有3个危险许可WRITE_EXTERNAL_STORAGE,READ_SMS& CAMERA许可。 任何人都可以告诉我哪里出错了。
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_SMS},
MY_PERMISSIONS_REQUEST_SMS);
}
}
}
}
答案 0 :(得分:0)
我认为你必须使用
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
而不是
write()
答案 1 :(得分:0)
使用此代码可能会对您有所帮助
在您要打开权限请求对话框的位置使用此代码。
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
else{
Log.e("permission force:", "Here you code if anyone check never and denied request of Write External Storage");
}
}
} else if (requestCode == 2) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 2);
}else{
Log.e("permission force:", "Here you code if anyone check never and denied request of Camera");
}
}
}else if (requestCode == 3) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_SMS)) {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, 3);
}else{
Log.e("permission force:", "Here you code if anyone check never and denied request of ReadSMS");
}
}
}
}
下面的代码用于处理权限对话框响应(再次打开权限对话框,授予或拒绝权限检查等)。
$totalRequests = \App\LoanRequest::all();
$filterMeta = [];
foreach($totalRequests as $applicant){
array_push($filterMeta, $applicant->aplicant->region);
}
答案 2 :(得分:0)
运行时权限API的一个更令人困惑的方面是shouldShowRequestPermissionRationale()
仅用于在PERMISSION_DENIED
回调中获得onRequestPermissionsResult()
时使用。在其他地方这样做会导致奇怪的行为。
您最好的选择是检查是否需要权限并请求,如果您的应用没有。如果需要,在结果处理拒绝。
您可能会发现此帮助程序库在处理此问题时更易于处理,也可帮助您的应用遵循适用于您的Google指南:https://github.com/hiqes/andele