Alarm manager
The alarm manager holds the processor tracking lock while the onReceive() receiver is onReceive() . This ensures that the phone will not sleep until you finish working with the broadcast. As soon as onReceive() returns, the alarm manager releases this tracking lock. This means that the phone will sleep in some cases as soon as your onReceive() method completes. If your alarm receiver called Context.startService() , it is possible that the phone will sleep before starting the requested service. To avoid this, your BroadcastReceiver and service will need to implement a separate tracking lockout policy to ensure that the phone continues to work until the service is available.
ScheduledThreadPoolExecutor.
You can use java.util.Timer or ScheduledThreadPoolExecutor (preferred) to schedule an action that happens at regular intervals in the background thread.
Here is an example with the latter:
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate (new Runnable() { public void run() {
So I preferred ScheduledExecutorService
But if updates will be performed while your application is running, you can use a timer, as suggested in other answers, or the new ScheduledThreadPoolExecutor. If your application will be updated, even if it is not running, you should go with AlarmManager.
The alarm manager is designed for cases where you want your application code to run at a specific time, even if your application is currently not working.
Please note that if you plan to update when the application is disconnected, then every 10 minutes are quite frequent and may be too energy-intensive.
Also check out this post.
source share