PotatoesPower PotatoesPower - 2 months ago 21
Java Question

Execute a task all x seconds

I have an

class
who send notification when my value
live == true
.

But how i can do for :
Launch every x seconds this activity in background.

Exemple : If the app is in background task but not running, she will execute this class all x seconds in background.

This app send a notification when my value is on
true
. But i need to refresh this result all 15m (15000 ms i think).

Thank you for your help.

Answer

First of all, you need to check the Activity lifecycle so that you will know how to determine if an activity is in the background.

https://developer.android.com/training/basics/activity-lifecycle/index.html

Once your are clear with that, you can use ScheduledExecutorService to schedule your class.

https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html