Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between AlarmManager and ScheduledExecutorService

Besides setting and exact time (i.e. midnight) versus setting a delay (i.e. 24 hours), what's the difference between using AlarmManager and ScheduledExecutorService to run a task periodically?

In my case, I need to run a little bit of code to check for new data every night and create a new notification if there is new data.

Thanks!

like image 864
Computerish Avatar asked Jul 02 '11 18:07

Computerish


People also ask

How is AlarmManager different from WorkManager?

Alarms only. Unlike WorkManager, AlarmManager wakes a device from Doze mode. It is therefore not efficient in terms of power and resource management. Only use it for precise alarms or notifications such as calendar events — not background work.

Does Android have a Scheduler?

The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the JobScheduler class. This API allows to batch jobs when the device has more resources available. In general this API can be used to schedule everything that is not time critical for the user.


3 Answers

ScheduledExecutorService runs in your application process. If application process dies, none of the scheduled tasks will run. Hence the need for Service (so your process lives beyond Activities active part of lifecycle).

While AlarmManager is critical system service that runs all the time. And if your application scheduled something and was killed, then AlarmManager may start application again (via PendingIntent).

And the last major difference that no one mentioned here is that AlarmManager knows about WakeLocks and power management. This means that AlarmManager may wake up Android device at specified time to run scheduled task. While ScheduledExecutorService knows nothing about power management and will only start task when device is not in deep sleep (i.e. it can simply miss the time).

like image 162
inazaruk Avatar answered Oct 16 '22 11:10

inazaruk


ScheduledExecutorService will only work if you have some component, such as a Service, running all of the time. Hence, it should only be used in cases where the component would be in memory for other reasons, adding value to the user. Having a component be in memory solely to watch the clock tick by is wasteful and one of the reasons why users attack developers with task killers and such.

AlarmManager is an OS-supplied system service. It can start up a component when the time rolls around. Hence, you do not need to have the component running.

In my case, I need to run a little bit of code to check for new data every night and create a new notification if there is new data.

This is a clear scenario for AlarmManager.

like image 34
CommonsWare Avatar answered Oct 16 '22 13:10

CommonsWare


I think ScheduledExecutorService is tied to your process and will not work in case your process gets killed. In contrast AlarmManager is managed by the OS so it works even if your application is not running.

like image 5
mibollma Avatar answered Oct 16 '22 12:10

mibollma