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 WakeLock
s 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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…