2017-12-18 70 views
1

我想要求用戶提供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();

我不知道爲什麼我得到這個錯誤,即使我已經調試了全班。 你能幫我找出錯誤及其解決方案嗎?

回答

1

通過重新檢查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();