Question

I a'm creating an Android application in which I'm managing some remainders. I want that when a certain event occur the dialog is show, And this is not a problem from me. But I want that if the user does not make any response within two minutes the dialog automatically dismiss. How can I implement this?

Was it helpful?

Solution

static AlertDialog alert = ....;


alert.show();

Runnable dismissRunner = new Runnable() {
    public void run() {
        if( alert != null )
            alert.dismiss();            
    };
new Handler().postDelayed( dismissRunner, 120000 );

Don't forget to alert = null in your regular dialog dismiss code (i.e. button onClick).

OTHER TIPS

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.game_message);
        game_message = builder.create();
        game_message.show();


        final Timer t = new Timer();
        t.schedule(new TimerTask() {
            public void run() {
                game_message.dismiss(); // when the task active then close the dialog
                t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
            }
        }, 5000);

You should be able to do just that using Timer:

http://developer.android.com/reference/java/util/Timer.html

Android timer? How-to?

The stackoverflow link describes how to use it to run a reoccuring task, but you can use it to run a one-shot task as well.

final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        alert.dismiss(); 
                        t.cancel(); 
                    }
                }, 2000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top