每次用户进入应用程序时,onResume()都在呼叫。如果用户在警报对话框中没有响应,则对话框只会累积。我想检查是否已显示对话框。警报对话多次呼叫
我发现这里给予了极大的答案:Prevent dialog to be opened multiple times when resuming activity这里How do I show only one Dialog at a time?但总是被一些错误而实施,在我的代码
警报对话框
private AlertDialog.Builder showGPSDialog(Context con) {
AlertDialog.Builder builder = new AlertDialog.Builder(con);
builder.setTitle("Enable GPS");
builder.setMessage("Please enable GPS");
builder.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(
new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.setNegativeButton("Ignore", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder;
}
的onResume()
LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean isEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isEnabled) {
showGPSDialog(MapsActivity.this).show();
}