Search code examples
androidandroid-timepicker

Countdown to Action


I would like my application to do an action after a certain amount of time has elapsed (preferablely while the app isn't open or paused).

Example:

If(hours4 == elapsed){

    this.close();
}

Solution

  • It's quite simple. You have to start this task in background using Service. To make delay you can use AlarmManager. Here is example

    or handler

    new Handler().postDelayed(new Runnable() {

        public void run() {
            Intent intent = new Intent("INTENT_WAKEUP_B");
                            intent.putExtra("EXTRA_MESSAGE",message);
                            sendBroadcast(intent);
        }
    }, timeToWait * 1000); // * 1000 if timeToWait is in seconds