我要求用戶閱讀聯繫人權限給所有閱讀聯繫人,並顯示在我已經做得很好的列表視圖中。如何在同一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)
發表您的logcat的錯誤... – rafsanahmad007