Anil M
Anil M

Reputation: 1307

Display AlertDialog in android after specified time

I'm in a situation where, AlertDialog should pop up after specific time.

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

In the code above, when Yes is clicked, customer would be logged of automatically after one minute. When No is clicked, no action would be taken, but after some time, the alert should pop again, i.e, whenever customer clicks No, AlertDialog should appear after specified time. Is there any way to do it?

Upvotes: 0

Views: 4164

Answers (3)

Anil M
Anil M

Reputation: 1307

This worked!!

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

}

Upvotes: 0

Rajeev
Rajeev

Reputation: 1404

Or you can use Timer class too to achieve this. When the timer fires you can show the dialog. Here is the reference schedule (TimerTask task, long delay)

Upvotes: 1

dd619
dd619

Reputation: 6170

Try this,

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

                    switch (msg.what)
                        {

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

                };
        };

And to show your AlertDialog send a message to above handler as,

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

Upvotes: 2

Related Questions