如何以编程方式删除android中的alertDialog.builder

时间:2016-12-22 11:16:09

标签: android alertdialog android-alertdialog

我有一个示例代码,用于显示每20秒保存在调度程序中的弹出消息(alertDialog.butiler)。问题是每隔20秒就会在旧弹出窗口中覆盖一个新窗口弹出窗口,所以我怎么能克服这个问题,我需要弹出一个弹出窗口,如果弹出窗口还活着的话不应生成新的弹出窗口。所以,请帮助我。

这是我的示例代码:

import android.support.v7.app.AlertDialog;
import android.content.DialogInterface;


ScheduledFuture<?> s = null;
private ScheduledExecutorService scheduler = null;
scheduler=Executors.newScheduledThreadPool(1);
s=scheduler.scheduleAtFixedRate(new

Runnable() {
     @Override
     public void run () {
            System.out.println("Called the scheduler");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (showAlertDialog) {
                        if (!isFinishing()) {
                            System.out.println("Called the scheduler on ui thread");
                            AlertDialog.Builder builder = new AlertDialog.Builder(WaitForRiderAllocationActivity.this);
                            builder.setCancelable(false);
                            builder.setTitle("TRIP UPDATES");
                            builder.setMessage("All are our riders are busy, do you want to still continue?");
                            builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                    finish();
                                }
                            });
                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(WaitForRiderAllocationActivity.this, "some text", Toast.LENGTH_LONG).show();
                                    Intent intent = new Intent(WaitForRiderAllocationActivity.this, GiveRideTakeRideActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                            builder.show();
                        }
                    }
                }

                );
            }
        },
        0, 20, TimeUnit.SECONDS);

2 个答案:

答案 0 :(得分:2)

继续参考您显示的对话框:

 if(mDialog!=null) {
      mDialog.dismiss();
      mDialog = null;
 }
 . . .
 mDialog = builder.show();

答案 1 :(得分:0)

保持AlertBuilder对象全局并在调度程序内检查构建器对象是否为null并创建对话框

// Global object
AlertBuilder builder; 


...
    s = scheduler.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        System.out.println("Called the scheduler");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (showAlertDialog){
                    if(!isFinishing()){
                        System.out.println("Called the scheduler on ui thread");
                        if(builder == null){
                            builder = new AlertDialog.Builder(WaitForRiderAllocationActivity.this);                         builder.setCancelable(false);
                            builder.setTitle("TRIP UPDATES");
                            builder.setMessage("All are our riders are busy, do you want to still continue?");
                            builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                    finish();
                                }
                            });
                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(WaitForRiderAllocationActivity.this,"some text",Toast.LENGTH_LONG).show();
                                    Intent intent = new Intent(WaitForRiderAllocationActivity.this, GiveRideTakeRideActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                            builder.show();
                        }
                    }
                }
            });
        }
    },
    0, 20,
            TimeUnit.SECONDS);