[ACCEPTED]-Which is Better ScheduledExecutorService or AlarmManager in android?-scheduledexecutorservice
Alarm Manager
The Alarm Manager holds a CPU wake lock 30 as long as the alarm receiver's onReceive()
method 29 is executing. This guarantees that the phone 28 will not sleep until you have finished handling 27 the broadcast. Once onReceive()
returns, the Alarm 26 Manager releases this wake lock. This means 25 that the phone will in some cases sleep 24 as soon as your onReceive()
method completes. If your 23 alarm receiver called Context.startService()
, it is possible that 22 the phone will sleep before the requested 21 service is launched. To prevent this, your 20 BroadcastReceiver
and Service will need to implement a separate 19 wake lock policy to ensure that the phone 18 continues running until the service becomes 17 available.
ScheduledThreadPoolExecutor.
You can use java.util.Timer or ScheduledThreadPoolExecutor
(preferred) to schedule 16 an action to occur at regular intervals 15 on a background thread.
Here is a sample 14 using the latter:
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate
(new Runnable() {
public void run() {
// call service
}
}, 0, 10, TimeUnit.MINUTES);
So I preferred ScheduledExecutorService
But 13 if the updates will occur while your application 12 is running, you can use a Timer, as suggested 11 in other answers, or the newer ScheduledThreadPoolExecutor. If 10 your application will update even when it 9 is not running, you should go with the AlarmManager.
The 8 Alarm Manager is intended for cases where 7 you want to have your application code run 6 at a specific time, even if your application 5 is not currently running.
Take note that 4 if you plan on updating when your application 3 is turned off, once every ten minutes is 2 quite frequent, and thus possibly a bit 1 too power consuming.
Also check out this post.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.