Reputation: 71
I'm working on an app which needs to send data to a webserver after every x minutes (where x is a value that can be changed through webserver responses).
Right now, I'm using a background thread that sleeps for x minutes and then notifies the main thread to start sending data.
However, I'm experiencing issues with this method due to CPU-sleeping, which seems to affect the thread sleeping time.
I've read about wakelocks and think I could implement a partial wakelock...however, one of the core features of this app is to save battery, so overall wakelocks just aren't feasible.
So I was wondering if there are any alternatives to wakelocks that can make sure that a certain piece of logic always runs in the background after every x minutes, ideally only waking up the CPU when it is needed?
Thanks in advance.
Upvotes: 4
Views: 1774
Reputation: 5781
Use SheduledThreadPoolExecutor
for this. It can handle single tasks, periodic tasks etc...
Upvotes: 2