拒绝所有权限后无法继续
我想要求用户提供Camera, Location, Write Storage, CallPhone
权限,并在用户拒绝任何权限时显示警告对话框。拒绝所有权限后无法继续
但我的代码抛出此异常:
E/AndroidRuntime:致命异常:主要 过程:fwt_android.mpermissions,PID:4850 了java.lang.RuntimeException:不提供结果ResultInfo {谁= @ android:requestPermissions :, request = 1,result = -1,data = Intent {act = android.content.pm.action.REQUEST_PERMISSIONS(has extras)}} to activity {fwt_android.mpermissions/fwt_android.mpermissions.activities.Splash} :android.content.res.Resources $ NotFoundException:资源ID#0x0 at android.app.ActivityThread.deliverResults(ActivityThread.java:3699) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1393) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java .lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit。 main(ZygoteInit.java:616) 引起:android.content.res.Resources $ NotFoundException:资源ID#0x0 at android.content.res.Resources.getValue(Resources.java:1351) at android.content。 res.Resources.loadXmlResourceParser(Resources.java:2774) at android.content.res.Resources.getLayout(Resources.java:1165) at android.view.LayoutInflater.inflate(LayoutInflater.java:421) at android。 view.LayoutInflater.inflate(LayoutInflater.java:374) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatDialog.setContent查看(AppCompatDialog.java:83) at android.support.v7.app.AlertController.installContent(AlertController.java:226) at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:260) at android.app.Dialog.dispatchOnCreate(Dialog.java:394) at android.app.Dialog.show(Dialog.java:295) at fwt_android.mpermissions.activities.Splash.onRequestPermissionsResult(Splash.java:215) at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553) at android.app.Activity.dispatchActivityResult(Activity.java:6432) at android.app.ActivityThread.deliverResults(ActivityThread.java:369 5) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1393 )android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread。java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os .ZygoteInit.main(ZygoteInit.java:616)
下面是用于检查权限的常量 -
private final int PERMISSION_CALLBACK_CONSTANT = 1; private final int REQUEST_PERMISSION_SETTING = 1;
String[] permissionsRequired = new String[]{Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE};
这里是我的检查permisions码 -
public void checkPermission() { if (ActivityCompat.checkSelfPermission(context, permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(context, permissionsRequired[1]) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(context, permissionsRequired[2]) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(context, permissionsRequired[3]) != PackageManager.PERMISSION_GRANTED
) {
if (ActivityCompat.shouldShowRequestPermissionRationale(Splash.this, permissionsRequired[0])
|| ActivityCompat.shouldShowRequestPermissionRationale(Splash.this, permissionsRequired[1])
|| ActivityCompat.shouldShowRequestPermissionRationale(Splash.this, permissionsRequired[2])
|| ActivityCompat.shouldShowRequestPermissionRationale(Splash.this, permissionsRequired[3])) {
//Show Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Need Multiple Permissions");
builder.setMessage("This app needs Camera Location, Storage and Phone permissions.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ActivityCompat.requestPermissions(Splash.this, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).create().show();
} else if (permissionStatus.getBoolean(permissionsRequired[0], false)
|| permissionStatus.getBoolean(permissionsRequired[1], false)
|| permissionStatus.getBoolean(permissionsRequired[2], false)
|| permissionStatus.getBoolean(permissionsRequired[3], false)
) {
//Previously Permission Request was cancelled with 'Dont Ask Again',
// Redirect to Settings after showing Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Need Multiple Permissions");
builder.setMessage("This app needs Camera, Location, Storage and Phone permissions.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
sentToSettings = true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
// Toast.makeText(getApplicationContext(), "Go to Permissions to Grant Camera and Location", Toast.LENGTH_LONG).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
finish();
}
}).create().show();
} else {
//just request the permission
ActivityCompat.requestPermissions(Splash.this, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
}
SharedPreferences.Editor editor = permissionStatus.edit();
editor.putBoolean(permissionsRequired[0], false);
editor.putBoolean(permissionsRequired[1], false);
editor.putBoolean(permissionsRequired[2], false);
editor.putBoolean(permissionsRequired[3], false);
editor.apply();
} else {
//You already have the permission, just go ahead.
splashTread.start();
}
}
这里是回调函数 -
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_CALLBACK_CONSTANT) {
//check if all permissions are granted
boolean allgranted = false;
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
allgranted = true;
} else {
allgranted = false;
break;
}
}
if (allgranted) {
splashTread.start();
} else if (ActivityCompat.shouldShowRequestPermissionRationale(Splash.this, permissions[0])
|| ActivityCompat.shouldShowRequestPermissionRationale(Splash.this, permissions[1])
|| ActivityCompat.shouldShowRequestPermissionRationale(Splash.this, permissions[2])
|| ActivityCompat.shouldShowRequestPermissionRationale(Splash.this, permissions[3])) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Need Multiple Permissions");
builder.setMessage("This app needs Camera, Location, Storage and Phone permissions.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(Splash.this, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
finish();
}
}).create().show();
} else {
Toast.makeText(getBaseContext(), "Unable to get Permission", Toast.LENGTH_LONG).show();
}
}
}
而且splashThread()
功能是当所有的权限被授予这是工作正确等功能,但是当我拒绝任何许可,并继续使新的警报对话框应显示发送用户设置,但应用程序崩溃在builder.show();
我不知道为什么我得到这个错误,即使我已经调试了全班。 你能帮我找出错误及其解决方案吗?
回答:
通过重新检查Google的运行时权限准则,为我的答案找到了解决方案。作为其既定there的
ActivityCompat.shouldShowRequestPermissionRationale()
不会阻碍线程的用户响应它应该只显示用户许可的要求作出解释。
所以,我做到了用checkPermission()
调用替换
AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Need Multiple Permissions");
builder.setMessage("This app needs Camera, Location, Storage and Phone permissions.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(Splash.this, permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
finish();
}
}).create().show();
以上是 拒绝所有权限后无法继续 的全部内容, 来源链接: utcz.com/qa/261779.html