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!
ScheduledExecutorServiceruns in your application process. If application process dies, none of the scheduled tasks will run. Hence the need forService(so your process lives beyond Activities active part of lifecycle).While
AlarmManageris critical system service that runs all the time. And if your application scheduled something and was killed, thenAlarmManagermay start application again (viaPendingIntent).And the last major difference that no one mentioned here is that
AlarmManagerknows aboutWakeLocks and power management. This means thatAlarmManagermay wake up Android device at specified time to run scheduled task. WhileScheduledExecutorServiceknows nothing about power management and will only start task when device is not in deep sleep (i.e. it can simply miss the time).