Java – Run the service in a separate thread and wake up every 10 minutes?

Run the service in a separate thread and wake up every 10 minutes?… here is a solution to the problem.

Run the service in a separate thread and wake up every 10 minutes?

My app will access the

network service every 10 minutes to access the data (even if the app is not in the foreground). What’s the best way to do it?

Do I need to start my service in a separate thread first?
And how to get it to get updates from the server every 10 minutes? Some people say handler.postdelayed, while others use Alarm Manager. Which is better, do we have some examples.

Solution

If the update will occur while your application is running, you can use a timer, as suggested in the other answers, or the updated one ScheduledThreadPoolExecutor .
If your application updates even when it is not running, you should use 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.

Note that if you plan to update when you close the app, the frequency of every ten minutes is very high, so it may be a bit too power-hungry.

Related Problems and Solutions