/ android

Scheduling tasks in Android made easy

Over the years Android has introduced several scheduling APIs for jobs that need to be scheduled outside the scope of an application’s lifecycle. Most of these come along with features that improve battery life & optimise resource utilisation. The choice of one suitable API the inflexibility of switching between them and the amount of boilerplate code required for setting up makes it difficult to use these APIs. On top of this API changes with varying Android API versions and most of the APIs not being backward compatible makes scheduling a headache.

Our Variable Frequency model is based on the premise that instead of collecting and transmitting GPS data at a single static frequency we can interpret the context and push corresponding commands to our DriverSDK to collect and transmit at varying frequencies at different points in time. For this model to work we needed a simpler and reliable API to schedule jobs. This is when we built Smart Scheduler an open-source scheduling library which helps you focus on getting the application right without having to spend hours on setting up job scheduling and handling all the nuances.

Overview of existing Scheduling APIs

Here is a quick intro along with pros and cons of the APIs that Android provides.

  1. AlarmManager
    AlarmManager is used if the periodic jobs need to run even when the app is not in the foreground. This leverages the alarm service on the phone to cause periodic executions of a service which will run continuously until explicitly stopped.

+ Available on all devices and all OS version
+ Easier to send a Broadcast to start a service delayed using this API
 API changes with changing Android API Versions
Periodic Alarms only work in case the interval between consecutive occurrences is  greater than 60 seconds
Alarms get reset on device reboot and hence need to be explicitly re-initiated

  1. Handler
    Handler is used to scheduling on a given thread after a delay or repeat tasks periodically on a thread. This is done by constructing a Handler and then “posting” runnable code to the event message queue on the thread to be processed.

+ Preferred for normal timing operations (ticks timeouts etc)
+ Suitable where scheduling needs to happen with an interval < 1 second
Doesn’t work efficiently while the app is in the background or not running at the moment.

  1. JobScheduler
    JobScheduler enables performing background work especially in an efficient way. Jobs are scheduled based on specified criteria such as device is charging idle connected to a network or connected to an unmetered network. They are queued up in the system to be performed later when these criteria are met. Along with this Android also tries to batch these jobs together to save battery life when making a network connection.

+ Relatively easier to use and provides customizations for the developer to schedule jobs better
+ Respects device state and prevents exploitation of resources by apps
Only available on Android Lollipop and above (21+)
 Jobs with network dependency don’t get scheduled if the periodic interval < 30 seconds
 Jobs stop getting scheduled in Power-Saver Mode if network connectivity is a dependency

  1. GcmNetworkManager
    GcmNetworkManager was introduced with all the nice battery saving features from JobScheduler but with backward compatibility and simplicity of its Service counterpart. Uses JobScheduler API behind the scenes for API level 21+ and features support for previous Android versions using Google Play Services.

+ Supported on Android Gingerbread and above (9+)
 Only available on devices with Google Play Services pre-installed
 Jobs with network dependency don’t get scheduled if the periodic interval < 30 seconds
Jobs stop getting scheduled in Power-Saver Mode if network connectivity is a dependency

Woah! The choice of an API alone seems a bit of a task now doesn’t it?

Introducing Smart-Scheduler


Depending on the requirements the device state and the Android API version smart-scheduler-android library decides the best-suited API for the job. It provides a superset of all features from the existing AlarmManager GcmNetworkManager and Handler APIs. Additionally it removes the boilerplate code & the need to worry about maintaining scheduling across devices and API versions.

Evernote has a similar scheduling library which unifies AlarmManager GcmNetworkManager & JobScheduler APIs. But when it comes to implementing network dependent jobs for an interval less than 30sec or implementing jobs while the device is in PowerSaver Mode it fails.

The library is available on jcenter() and mavenCentral() and to set it up for your project you can follow the steps as highlighted here. Once this is done you can start scheduling your jobs! You don’t need to declare any services receivers or permissions in your manifest. The Readme on Github details out the usage. The Demo App will help you see the library in action.

Smart-Scheduler Demo

Have issues or feature requests? Use issues tracker. We’d love to see your pull requests so send them in! Have questions suggestions? Join the discussion on slack.

Like what we are doing? Sign up to use HyperTrack and build location tracking features!