AlertDialog未被驳回

时间:2012-12-11 02:55:26

标签: android alertdialog android-alertdialog

我有以下代码来显示一个警告对话框,用于打开位置服务设置:

alert = new AlertDialog.Builder(context).create();
alert.setTitle("Unable to Retrieve Current Location");
alert.setMessage(" Enable Location Service ?");
alert.setButton(DialogInterface.BUTTON_POSITIVE,"Open Settings", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        alert.dismiss();
        Intent intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
        context.startActivity(intent);

    }
});
alert.setCancelable(false);
alert.show();

设置正常打开,但当我按回时,“上一个”对话框不会被取消。从设置返回时如何关闭对话框。

3 个答案:

答案 0 :(得分:0)

试试这个:

AlertDialog.Builder alert = new AlertDialog.Builder (context);
alert.setTitle ("Unable to Retrieve Current Location");
alert.setMessage (" Enable Location Service ?");
alert.setButton (DialogInterface.BUTTON_POSITIVE, "Open Settings", 
                 new DialogInterface.OnClickListener() {

  @Override
  public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
    context.startActivity (intent);
  });

// Create and show the dialog.
alert.create();
alert.show();

主要区别在于推迟创建,直到您添加组件,调用 show ,而不是调用 dismiss

答案 1 :(得分:0)

而不是alert.dismiss();

点击活动

尝试finish(); //这将完成您的活动

答案 2 :(得分:0)

使用dialog.dismiss();代替alert.dismiss();