If you have been following this series you may have noticed already we haven’t found any Andoird component that can handle a background task to execute at the exact given time. The closest we come to achieving something like that is by using PeriodicWorkRequestBuilder
in WorkManager
. Even when using WorkManager
that it won’t wake your phone from Doze mode. AlarmManager
is the suitable component that can be used to execute a background task in a fixed time in the future.
How does AlarmManager work?
AlarmManager
allows developers to schedule tasks that must be executed at a specific time or after a specific interval. It is a system service that runs in the background and handles all the scheduled tasks. When an alarm is triggered, the AlarmManager
sends a broadcast intent to the system, which starts the specified service or activity. The AlarmManager
Itself is not capable of running a background task but rather capable of triggering a component that is capable of executing a background task. commonly uses Service with AlarmManager for executing background tasks.
One of the most significant advantages of using AlarmManager
is that it allows developers to schedule tasks even when the application is not running. The only downside is that AlarmManager
will get reset if the device is restarted. But there is a workaround for that which we will discuss in this article.