Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a service in separate thread and waking it every 10 minutes?

My app will access a webservice to access data (even if the application is not in foreground), every 10 minutes. What is the best way to do that?

First do I need to start my service in seaparate thread? And how to make it get update from server every 10 minutes? Some people said handler.postdelayed and some using Alarm Manager. which one is better and do we have some examples.

like image 836
carora3 Avatar asked Nov 14 '25 22:11

carora3


1 Answers

If the updates will occur while your application is running, you can use a Timer, as suggested in other answers, or the newer ScheduledThreadPoolExecutor.
If your application will update even when it is not running, you should go with the AlarmManager:

The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

Take note that if you plan on updating when your application is turned off, once every ten minutes is quite frequent, and thus possibly a bit too power consuming.

like image 142
Jave Avatar answered Nov 17 '25 11:11

Jave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!