Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic dialog dismiss in Android

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?

like image 817
GVillani82 Avatar asked Mar 06 '26 17:03

GVillani82


2 Answers

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).

like image 140
323go Avatar answered Mar 09 '26 07:03

323go


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);
like image 36
Hazel Beams Avatar answered Mar 09 '26 07:03

Hazel Beams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!