2016-12-26 37 views
0

我要求用戶閱讀聯繫人權限給所有閱讀聯繫人,並顯示在我已經做得很好的列表視圖中。如何在同一Android活動中第二次使用PERMISSION_GRANTED獲得相同的運行時權限?

現在我試圖做的一樣,如果用戶拒絕讀取的所有聯繫,我會自動顯示警告對話框中,如果用戶按是那麼用戶會重定向到PERMISSION_GRANTED了第二次,如果他授予的許可,則用戶會重定向到其他片段,但那時我面對錯誤。我下面的代碼:

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     switch (requestCode){ 
      case REQUEST_CODE: 
       if(grantResults[0]==PackageManager.PERMISSION_GRANTED) { 

        if(InfoSetClass.getDialogCode()==0) { 
         logIn(); 
         contacts(); 
        } 
        else { 

         Fragment fragment=new TestFragment(); 
         FragmentManager fragmentManager=getSupportFragmentManager(); 
         FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); 
         fragmentTransaction.add(R.id.activity_main,fragment); 
         fragmentTransaction.commit(); 

        } 
       }else if(grantResults[0]==PackageManager.PERMISSION_DENIED){ 

        InfoSetClass.setDialogCode(1); 

        AlertDialog.Builder builder=new AlertDialog.Builder(SignUpActivity.this); 

        builder.setMessage("Want to select persons to invite?"); 
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 

          if (ActivityCompat.checkSelfPermission(context_SignUpActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 

           ActivityCompat.requestPermissions(SignUpActivity.this, new String[]{ 
             Manifest.permission.READ_CONTACTS 
           }, REQUEST_CODE); 

          } else { 
           // permission granted 

           Fragment fragment=new TestFragment(); 
           FragmentManager fragmentManager=getSupportFragmentManager(); 
           FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); 
           fragmentTransaction.add(R.id.activity_main,fragment); 
           fragmentTransaction.commit(); 

          } 

         } 
        }); 
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 

          logIn(); 
         } 
        }); 

        AlertDialog alertDialog=builder.create(); 
        alertDialog.show(); 

       }break; 

     } 
    } 

的logcat:

FATAL EXCEPTION: main 
        Process: com.systechdigital.webadeal, PID: 17825 
        java.lang.RuntimeException: Failure delivering result ResultInfo{[email protected]:requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.systechdigital.webadeal/com.systechdigital.webadeal.SignUpActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
         at android.app.ActivityThread.deliverResults(ActivityThread.java:4998) 
         at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041) 
         at android.app.ActivityThread.access$1600(ActivityThread.java:229) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:148) 
         at android.app.ActivityThread.main(ActivityThread.java:7325) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
        Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
         at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1533) 
         at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1551) 
         at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696) 
         at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662) 
         at com.systechdigital.webadeal.SignUpActivity.onRequestPermissionsResult(SignUpActivity.java:297) 
         at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7291) 
         at android.app.Activity.dispatchActivityResult(Activity.java:7169) 
         at android.app.ActivityThread.deliverResults(ActivityThread.java:4994) 
         at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)  
         at android.app.ActivityThread.access$1600(ActivityThread.java:229)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:148)  
         at android.app.ActivityThread.main(ActivityThread.java:7325)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  
+0

發表您的logcat的錯誤... – rafsanahmad007

回答

0

你可以試試這個。希望能幫助到你。

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] 
grantResults) { 
    switch (requestCode) { 
     case REQUEST_CODE: { 
      // If request is cancelled, the result arrays are empty. 
      // String permission = permissions[0]; 

       String permission = permissions[0]; 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        Fragment fragment=new TestFragment(); 
        FragmentManager 
        fragmentManager=getSupportFragmentManager(); 
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); 
        fragmentTransaction.add(R.id.activity_main,fragment); 
        fragmentTransaction.commit(); 

       } else { 

        if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) { 

         // user denied WITH never ask again 
         showSettingDialog(); 


        } else if (Manifest.permission.READ_CONTACTS.equals(permission)) { 
         showPermissionDialog(); 
         // user denied WITHOUT never ask again 
         // this is a good place to explain the user 
         // why you need the permission and ask if he want 
         // to accept it (the rationale) 
        } 


       } 
       return; 

     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

代碼showPermissionDialog()

private void showPermissionDialog() { 

    final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create(); 
    dialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 

      dialog.cancel(); 
      ActivityCompat.requestPermissions(MainActivity.this, 
        new String[]{Manifest.permission.READ_CONTACTS}, 
        MY_PERMISSIONS_REQUEST_READ_LOCATION); 

     } 
    }); 
    dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      dialog.cancel(); 
     } 
    }); 

    dialog.setTitle("Read Contacts explaination"); 
    dialog.setMessage("Explaination..."); 
    dialog.show(); 
} 

代碼showSettingDialog()

private void showSettingDialog() { 

    final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create(); 
    dialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 


      Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
      Uri uri = Uri.fromParts("package", getPackageName(), null); 
      intent.setData(uri); 
      startActivityForResult(intent, REQUEST_PERMISSION_SETTING); 
      dialog.cancel(); 

     } 
    }); 
    dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      dialog.cancel(); 
     } 
    }); 

    dialog.setTitle("Permission Denied"); 
    dialog.setMessage("Please enable Contacts from your app setting screen"); 
    dialog.show(); 
} 
0

你可以使用:

public abstract int commitAllowingStateLoss() 

,而不是commit ..

從文檔:

贊提交(),但允許提交要執行的活動的 狀態保存後。這很危險,因爲如果 該活動需要稍後從其狀態恢復,則該提交可能會丟失,所以這個應該只用於UI狀態在用戶意外改變 的情況。

所以試試這個:

Fragment fragment=new TestFragment(); 
    FragmentManager fragmentManager=getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.activity_main,fragment); 
    fragmentTransaction.commitAllowingStateLoss(); 
+0

現在顯示此錯誤 java.lang.IllegalArgumentException異常:ID:未找到ID 0x7f0c007e(com.systechdigital.webadeal視圖/ activity_main)片段 但片段工作正常,我測試從其他方式。 也不工作,如果條件 – AAA

+0

嘗試把你的容器編號在你想替換 – rafsanahmad007

+0

仍然錯誤layour .......... – AAA

相關問題