在指定时间后在android中显示AlertDialog

时间:2013-07-11 06:19:55

标签: android android-alertdialog

我遇到的情况是AlertDialog应该在特定时间后弹出。

 AlertDialog.Builder alertDialog = new AlertDialog.Builder(PDFDisplayActivity.this);
          alertDialog.setTitle(" Auto Logout");
          alertDialog.setMessage("You will be logged out automatically after 1 minute.");

          alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {

                 waitTimer = new CountDownTimer(60000, 1000) {

                    public void onTick(long millisUntilFinished) {
                        //Toast.makeText(getApplicationContext(), "seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_SHORT).show();
                    }
                    public void onFinish() {
                        Intent logout = new Intent(getApplicationContext(), LoginActivity.class);
                        logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(logout);
                        finish();
                    }
                 }.start();
              }
          });

          alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  //Toast.makeText(getApplicationContext(), "Please logout when you are done reading the agreement.", Toast.LENGTH_SHORT).show();

              }
          });
          alertDialog.show();

在上面的代码中,当点击Yes时,客户将在一分钟后自动记录。单击No时,不会采取任何操作,但在一段时间后,警报应再次弹出,即,只要客户点击NoAlertDialog应在指定时间后显示。有没有办法做到这一点?

4 个答案:

答案 0 :(得分:2)

试试这个,

public Handler Alerthandler = new Handler()
        {
            public void handleMessage(Message msg)
                {

                    switch (msg.what)
                        {

                        case 0:
                          //put your alertDialog code here
                            break;
                        }

                };
        };

并显示您的AlertDialog向上述处理程序发送消息为

Alerthandler.sendEmptyMessageAtTime(0,1000/*your time in millis*/);

答案 1 :(得分:1)

或者您也可以使用Timer类来实现此目的。当计时器触发时,您可以显示对话框。这是参考schedule (TimerTask task, long delay)

答案 2 :(得分:0)

这有效!!

public void alert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(PDFDisplayActivity.this);
  alertDialog.setTitle(" Auto Logout");
alertDialog.setMessage("You will be logged out automatically after 1 minute.");

alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {

     waitTimer = new CountDownTimer(60000, 1000) {

            public void onTick(long millisUntilFinished) {
                //Toast.makeText(getApplicationContext(), "seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_SHORT).show();
            }
            public void onFinish() {
                Intent logout = new Intent(getApplicationContext(), LoginActivity.class);
                logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(logout);
                finish();
            }
         }.start();
    }
});

alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(getApplicationContext(), "Please logout when you are done reading the agreement.", Toast.LENGTH_SHORT).show();
     waitTimer = new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {
                //Toast.makeText(getApplicationContext(), "seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_SHORT).show();
            }
            public void onFinish() {
                alert();
            }
         }.start();

    }
});
alertDialog.show();

}

答案 3 :(得分:0)

这是一个解决方案:

protected void activeProgressDialog() {
    final AlertDialog.Builder dialog = new    AlertDialog.Builder(PickUpActivity.this);
            dialog.setTitle("You Accepted the customer request");
            dialog.setMessage("The call center will call you");
            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                     dialog.dismiss();
                }
            });
    dialog.setCancelable(false);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            dialog.show();
        }
    }, 2500);
}