简体   繁体   English

如何在Android中以编程方式删除alertDialog.builder

[英]How to delete alertDialog.builder in android programmatically

I have a sample code to show a pop-up message(alertDialog.butiler) for every 20 seconds which is kept in the scheduler. 我有一个示例代码,用于每20秒显示一次弹出消息(alertDialog.butiler),该消息保留在调度程序中。 the problem is a new window pop-up is overwritten on the old pop-up for every 20 sec's, so how can I overcome this, I need to get a single pop-up and if that pop-up is alive the new pop-up window should not be generated. 问题是每20秒就会在旧弹出窗口上覆盖一个新的窗口弹出窗口,因此,如何克服这个问题,我需要获得一个弹出窗口,如果该弹出窗口还活着,则新弹出窗口不应生成向上窗口。 so, please help me. 所以,请帮帮我。

This is my sample code: 这是我的示例代码:

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);

Keep reference to dialog you have shown: 继续参考显示的对话框:

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

Keep the AlertBuilder object global and inside your scheduler check if the builder object is null and create your dialog 保持AlertBuilder对象为全局,并在计划程序内部检查builder对象是否为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM