如何从处理程序每​​次停止显示相同的警报对话框?

时间:2018-06-13 05:23:55

标签: java android alertdialog

我在方法上有 Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { handler.postDelayed(this, 1000); checkCountry(); } }; handler.postDelayed(runnable, 1000); public void checkCountry() { alertDialogueBuilder = new AlertDialog.Builder(MainActivity.this); alertDialogueBuilder.setTitle("VPN Detected!"); alertDialogueBuilder.setMessage("Please Turn Of VPN To Continue!"); alertDialogueBuilder.setIcon(R.drawable.errorstop); alertDialogueBuilder.setCancelable(false); alertDialogueBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); AlertDialog alertDialog = alertDialogueBuilder.create(); if(alertDialog.isShowing()){ //Do Something }else{ alertDialog.show(); } } ,方法在{{1}}中使用。每当{{1}}一次又一次地加载{{1}}时,如果对话框仍然显示我想要再次加载,我想要显示一次对话框。为此,我有以下代码,但没有工作。

{{1}}

3 个答案:

答案 0 :(得分:0)

仅创建一次Dialog,而不是每次都创建:

private AlertDialog alertDialog;
// ...
initDialog();
Handler handler = new Handler();
  Runnable runnable = new Runnable() {
    @Override
      public void run() {
        handler.postDelayed(this, 1000);
        checkCountry();
    }

};
handler.postDelayed(runnable, 1000);

//...

public void initDialog() {
     alertDialogueBuilder = new     AlertDialog.Builder(MainActivity.this);
     alertDialogueBuilder.setTitle("VPN Detected!");
     alertDialogueBuilder.setMessage("Please Turn Of VPN To Continue!");
     alertDialogueBuilder.setIcon(R.drawable.errorstop);
     alertDialogueBuilder.setCancelable(false);
     alertDialogueBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        alertDialog =     alertDialogueBuilder.create();
}

public void checkCountry() {
     if(alertDialog.isShowing()){
     //Do Something
     }else{
         alertDialog.show();
     }
}

答案 1 :(得分:0)

要仅显示1次对话框,请仅调用此checkGeoun()方法,您要从中显示此对话框。并且,请删除处理程序代码。这里不需要使用Handler。仅使用checkCountry()方法显示对话框。

答案 2 :(得分:0)

本书中最古老的技巧是使用假初始化创建一个布尔字段“isAlertDialogShown”,在创建时为true,并在onClick中再次将其设置为false(如果您希望在处理程序触发时再次显示它)

private boolean isShown = false;    
public void checkCountry() { 
            if (isShown){
             //do something
             return;
             }
            isShown = true;
            alertDialogueBuilder = new     AlertDialog.Builder(MainActivity.this);
            alertDialogueBuilder.setTitle("VPN Detected!"); 
                      alertDialogueBuilder.setMessage("Please Turn Of VPN To Continue!"); 
   alertDialogueBuilder.setIcon(R.drawable.errorstop);
      alertDialogueBuilder.setCancelable(false); 
            alertDialogueBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override 
                public void onClick(DialogInterface dialog, int which) {
                    isShown = false;
                    finish();
                } 
            }); 
            AlertDialog alertDialog =     alertDialogueBuilder.create();

             alertDialog.show();
   }

如果你想尝试使用alertDialog isShowing你需要使用你创建的那个而不是新的那个,所以再次将它保存为字段, 但是如果处理程序计时器运行得太快,你仍然可能有一个边缘情况,那就是alertDialog.show()不是立即操作:

AlertDialog alertDialog;
public void checkCountry() { 
            if ( alertDialog != null && alertDialog.isShowing){
                //do something
                return;                     
            }
            alertDialogueBuilder = new     AlertDialog.Builder(MainActivity.this);
            alertDialogueBuilder.setTitle("VPN Detected!"); 
                      alertDialogueBuilder.setMessage("Please Turn Of VPN To Continue!"); 
   alertDialogueBuilder.setIcon(R.drawable.errorstop);
      alertDialogueBuilder.setCancelable(false); 
            alertDialogueBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override 
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                } 
            }); 
            alertDialog =     alertDialogueBuilder.create();

            alertDialog.show();
   }