2016-12-24 23 views
0

我在ActionBar中有一個位置按鈕。點擊它將加載給定的功能cityLocation()位置權限對話框在活動中沒有正確顯示

代碼執行如下

  1. 按下時,第一次它要求的位置的權限
  2. 如果接受了它運行showCity(),否則它會彈出一個吐司
  3. 後如果再次點擊拒絕,它執行shouldShowRequestPermissionRationale()部分(PS。我沒有點擊不再顯示)

無論如何,這只是我第二次點擊按鈕,而是o f再次詢問位置權限(不再顯示覆選框),它執行shouldShowRequestPermissionRationale()部分。

我想讓代碼詢問位置權限與不再問複選框,如果權限第一次被拒絕,然後如果我再次拒絕它(與複選框),然後單擊操作欄項目,那麼它應該運行MaterialDialog.Builder代碼。

下面是我在現在的代碼:等待解決

private void cityLocation() { 
    if (Build.VERSION.SDK_INT >= 23) { 
     if (ActivityCompat.checkSelfPermission(WeatherActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(WeatherActivity.this, 
        Manifest.permission.ACCESS_COARSE_LOCATION)) { 
       MaterialDialog dialog; 
       MaterialDialog.Builder builder = new MaterialDialog.Builder(this); 
       builder.title("Permission needed") 
         .content("This Action Requires the Location Setting to be enabled. Go to Settings and check the Location Permission inside the Permissions View") 
         .positiveText("SETTINGS") 
         .negativeText("CANCEL") 
         .onPositive(new MaterialDialog.SingleButtonCallback() { 
          @Override 
          public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { 
           final Intent i = new Intent(); 
           i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
           i.addCategory(Intent.CATEGORY_DEFAULT); 
           i.setData(Uri.parse("package:" + getApplicationContext().getPackageName())); 
           i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
           i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
           i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
           startActivity(i); 
          } 
         }) 
         .onNegative(new MaterialDialog.SingleButtonCallback() { 
          @Override 
          public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { 
           dialog.dismiss(); 
          } 
         }); 
       dialog = builder.build(); 
       dialog.show(); 
      } 
      else 
       requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
         READ_COARSE_LOCATION); 
     } else { 
      showCity(); 
     } 
    } 
    else { 
     showCity(); 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             @NonNull String[] permissions, 
             @NonNull int[] grantResults) { 
    if (requestCode == READ_COARSE_LOCATION 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
     showCity(); 
    } 
    else { 
     Toast.makeText(getApplicationContext() , "Denied Location Permission" , Toast.LENGTH_SHORT).show(); 
    } 
} 

回答

1

這裏是完整的運行permisssion校驗碼:

private void cityLocation() { 
    if (Build.VERSION.SDK_INT >= 23) { 
     if (!checkIfAlreadyhavePermission()) { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, READ_COARSE_LOCATION); 
     } else { 
      showCity(); 
     } 
    } else { 
     showCity(); 
    } 
} 

private boolean checkIfAlreadyhavePermission() { 
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION); 
    return result == PackageManager.PERMISSION_GRANTED; 
} 

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case READ_COARSE_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       showCity(); 

      } else { 
       permission_denied(); 
      } 
      break; 
     } 
     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

public void permission_denied() { 
    // permission was not granted 
    //permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission 
    // shouldShowRequestPermissionRationale will return true 
    //show the dialog or snackbar saying its necessary and try again otherwise proceed with setup. 
    if (ActivityCompat.shouldShowRequestPermissionRationale(WeatherActivity.this, 
      Manifest.permission.ACCESS_COARSE_LOCATION)) { 
     showDialogOK("Permission is required for register", 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         switch (which) { 
          case DialogInterface.BUTTON_POSITIVE: 
           showCity(); 
           break; 
          case DialogInterface.BUTTON_NEGATIVE: 
           // proceed with logic by disabling the related features or quit the app. 
           break; 
         } 
        } 
       }); 
    } //permission is denied (and never ask again is checked) 
    //shouldShowRequestPermissionRationale will return false 
    else { 
     Toast.makeText(getApplicationContext(), "Go to settings and enable External storage permissions", Toast.LENGTH_LONG).show(); 
     showMaterialDialog(); 
    } 
} 

public void showMaterialDialog() { 
    MaterialDialog dialog; 
    MaterialDialog.Builder builder = new MaterialDialog.Builder(this); 
    builder.title("Permission needed") 
      .content("This Action Requires the Location Setting to be enabled. Go to Settings and check the Location Permission inside the Permissions View") 
      .positiveText("SETTINGS") 
      .negativeText("CANCEL") 
      .onPositive(new MaterialDialog.SingleButtonCallback() { 
       @Override 
       public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { 
        final Intent i = new Intent(); 
        i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
        i.addCategory(Intent.CATEGORY_DEFAULT); 
        i.setData(Uri.parse("package:" + getApplicationContext().getPackageName())); 
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
        i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
        startActivity(i); 
       } 
      }) 
      .onNegative(new MaterialDialog.SingleButtonCallback() { 
       @Override 
       public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { 
        dialog.dismiss(); 
       } 
      }); 
    dialog = builder.build(); 
    dialog.show(); 

} 

private void showDialogOK(String message, DialogInterface.OnClickListener okListener) { 
    new AlertDialog.Builder(getActivity()) 
      .setMessage(message) 
      .setPositiveButton("OK", okListener) 
      .setNegativeButton("Cancel", okListener) 
      .create() 
      .show(); 
} 
  1. 如果用戶拒絕權限,它會顯示一個對話框......

  2. 如果用戶同時選中不再詢問會顯示材料對話框..

  3. 如果用戶授予該權限,它將執行所需的功能..

+0

這對我有效..謝謝你先生 – Sparker0i

0

如果您在權限對話框中選中該複選框neverAskAgain,然後

ActivityCompat.shouldShowRequestPermissionRationale() 

方法將返回false。所以如果你把它放在if塊中,你再次顯示一個材質對話框的邏輯將不起作用。

如果必須再次顯示,請在else塊中執行。

+0

先生,我試過這個,但它給了我一個無限循環的權限對話框,當我點擊不再問,然後啓動材料對話框。 – Sparker0i