Hi I was reading on internet that we can schedule any thing which we want to run at a gap of certain interval using alarm manager and ScheduledExecutorService
.
I want to know what is the difference between them and what to use when
Thanks in advance.
ScheduledExecutorService
is something inside your process. If your process is already around for other reasons, such as it is supplying the foreground UI, and you want to use ScheduledExecutorService
for periodic work, that's fine. However, once your app moves to the background, its process may go away at any time, taking your ScheduledExecutorService
with it, and you will no longer get control periodically.
AlarmManager
is something outside your process. If you are looking for the Android equivalent of Windows' "Scheduled Tasks", or OS X/Linux cron
jobs, that is what AlarmManager
(or JobScheduler
on Android 5.0+) is for. These are for cases where you need application code to run periodically in the background but are happy to allow your process to possibly be terminated in between those periods.