Schedule Tasks in Android : all the way from AlarmManager to Firebase Job Dispatcher to Doze Mode

Thinking of implementing a scheduler in your android app? But confused in choosing the right one? There are, actually, a plenty of things to keep in mind when trying to choose the right scheduler for Android applications:

Which scheduler will NOT eat whole of the phone’s battery?

Will the app need the scheduling to exist outside the scope of the application lifecycle?

Will the app need to schedule on the basis of network availability?

Which scheduler will support most/all of the users? (backward compatible)

Google, over the past few years, has introduced many schedulers. Each with its own pros and cons. And hence it’s your call to decide which one to go for!

Lets have a look at the picture below:

If the job that your app need to schedule doesn’t need to live beyond the scope of your application, Google recommends using the Handler class along with a Timer and Thread. For any timing operations that occur only during the lifetime of your application, it is best to use application resources rather than system resources to schedule tasks. Using a Handler is easier and more efficient than the following methods when it comes to scheduling tasks within your application process.

AlarmManager

We have AlarmManager API since the very beginning of Android. Although significant changes were made in this API from version 19, to optimise battery usage and the performance, the main drawback of Alarm Manager is that it solely works on the basis of time. To overcome this, JobScheduler was introduced in API 21, which works on various conditions like availability of network, charging of device.

JobScheduler

JobScheduler provides efficient background scheduling, but only if your user is running API level 21 or higher (that’s about 25% of users at the time of writing this post).

Also, Google has stopped active development on this module and instead recommends to use Firebase Job Dispatcher for scheduling any tasks.

FirebaseJobDispatcher

Firebase Job Dispatcher library uses the scheduling engine inside Google Play services to provide a backwards compatible (back to Gingerbread) JobScheduler-like API. If Google Play Services app is not installed or the app version is less than 7.5, then this library internally uses AlarmManager to schedule the tasks.

Doze Mode

Doze Mode was recently introduced in Android Marshmallow as a way to minimise battery drain while a user has been away from their device for a period of time. Doze Mode is automatically enabled on devices running Android API level 23 and higher. It is not a scheduler that developers need to implement. Rather, it affects the other schedulers we have already discussed.

Figure . Doze provides a recurring maintenance window for apps to use the network and handle pending activities.

If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode. In Doze mode, the system attempts to conserve battery by restricting apps’ access to network and CPU-intensive services. It also prevents apps from accessing the network and defers their jobs, syncs, and standard alarms.Periodically, the system exits Doze for a brief time to let apps complete their deferred activities. During this maintenance window, the system runs all pending syncs, jobs, and alarms, and lets apps access the network.