Slide 1

Slide 1 text

Android Scheduling made easy Piyush Gupta Android Developer @ HyperTrack

Slide 2

Slide 2 text

Agenda ● Android Scheduling ● Android Scheduling APIs ● HyperTrack’s need for scheduling ● Smart-Scheduler Library ● Future Work ● Questions

Slide 3

Slide 3 text

Android Scheduling Modern apps can perform many of their tasks asynchronously, outside the direct flow of user interaction. Some examples of these asynchronous tasks are: ● Updating network resources. ● Downloading information. ● Updating background tasks. ● Scheduling system service calls. Scheduling this work intelligently can improve your app’s performance, along with aspects of system health such as battery life.

Slide 4

Slide 4 text

Android Scheduling APIs ● 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. ● Currently available APIs: ○ Handler ○ AlarmManager ○ JobScheduler ○ GcmNetworkManager

Slide 5

Slide 5 text

Handler ● Handler is used to schedule tasks either after a delay or repeat them periodically on a given thread. ● This is achieved 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 < 30 second − Doesn’t work efficiently while the app is in the background or not running at the moment.

Slide 6

Slide 6 text

AlarmManager ● AlarmManager is used if the periodic jobs need to run even when the app is not in the foreground. ● It 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 versions + Easier to send a Broadcast to start a service delayed using this API − There have been numerous changes with changing Android API Versions − Periodic Alarms only work in case the interval b/w consecutive occurrences is > 60sec − Alarms get reset on device reboot and hence need to be explicitly re-initiated

Slide 7

Slide 7 text

JobScheduler ● JobScheduler enables performing background work, esp. networking, 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 optimize resources. + Relatively easier to use & 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

Slide 8

Slide 8 text

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 APIs 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+) − Google Play Services is a prerequisite − Jobs with network dependency don’t get scheduled if the periodic interval < ~30 secs. − Jobs stop getting scheduled in Power-Saver Mode if network connectivity is a dependency

Slide 9

Slide 9 text

Need for an aggregated API ● Scheduling could encompass following types of jobs for interval ranging from 1sec to anything near 24 hours. − Network dependent (or independent) jobs − Periodic (or Nonperiodic) jobs Thus we needed a simpler & reliable API to schedule jobs on all devices and in all conditions.

Slide 10

Slide 10 text

Smart-Scheduler Library Quick Summary of issues: ● Choice of one suitable API (for all devices or Android versions) ● Inflexibility of switching between APIs ● Amount of boilerplate code required for setting up ● API changes with varying Android API versions ● APIs not being backward compatible This is when we built Smart Scheduler, an open-source scheduling library, which takes care of Scheduling APIs, their boilerplate code and nuances associated with each API.

Slide 11

Slide 11 text

Usage Refer to the Github repository for more info.