简体   繁体   中英

Display an Alert Dialog Box for custom time period in android

In my android app I want to display an Alert dialog box for a limited time period. Which means If user doesn't reply to alert dialog box within limited time period , alert box must be closed. I searched on the Internet about this but could not find a way. Is anyone aware about how to do this ?

Couple of options spring to mind You need to call the dialog dismiss() function right? so it's just a case of knowing when to call it.

Have a look at http://developer.android.com/reference/java/util/concurrent/ScheduledThreadPoolExecutor.html

and

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

The ScheduledThreadPoolExecutor in the first link looks like just the ticket for you.

The accepted answer here Where do I create and use ScheduledThreadPoolExecutor, TimerTask, or Handler? shows an example to how to use it. You would set it up in the onCreate of the Dialog class you are showing or at the time when you show the dialog in the calling class.

you can do like this

private final Runnable mDismissAction = new Runnable() {
    public void run() {
        dismissDialog();
    }
};

then you can dissmiss the dialog delay, use

handler.postDelayed(mDismissAction, delay);

when user click the dialog it will cancel the action, use

handler.removeCallbacks(mDissmissAction)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM