Countdown to Action

86 Views Asked by At

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

There are 2 best solutions below

1
Serafins On BEST ANSWER

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
0
Kuffs On

Use AlarmManager to schedule events to run at a future time.