我在ActionBar中有一個位置按鈕。點擊它將加載給定的功能cityLocation()
。位置權限對話框在活動中沒有正確顯示
代碼執行如下
- 按下時,第一次它要求的位置的權限
- 如果接受了它運行showCity(),否則它會彈出一個吐司
- 後如果再次點擊拒絕,它執行
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();
}
}
。
這對我有效..謝謝你先生 – Sparker0i