我在我的GPSTracker类中有这个方法:
public void showSettingsAlert() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(myContext);
// Setting Dialog Title
alertDialog.setTitle("GPS Settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.delete);
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
myContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
我运行它时,会同时弹出10个对话框,基本上我需要按下10次取消或类似的操作,该怎么解决? 我读到了关于
final AlertDialog alert = alertDialog.create();
if(alert.isShowing()) {
alert.dismiss();
}
else {
alert.show();
}
但这对我没有用,我仍然会得到多个对话框叠在一起...有人能帮帮我吗?基本上我在我的活动中调用if(gps.isEnabled()) gps.showSettingsAlert();
if (itemsArrayList.get(position).getCoordinates() == null || !gps.canGetLocation()) {
holder.distance.setText("Distance not available");
gps.showSettingsAlert();
}
showSettingsAlert
的代码处发布。 - Atiq