0

我处于一种情况,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

3 回答 3

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*/);
于 2013-07-11T06:34:37.697 回答
1

或者你也可以使用 Timer 类来实现这一点。当计时器触发时,您可以显示对话框。这是参考时间表(TimerTask 任务,长时间延迟)

于 2013-07-11T06:45:41.937 回答
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();

}

于 2013-07-11T07:38:37.387 回答