Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Scheduling made easy

Piyush Gupta
December 13, 2016

Android Scheduling made easy

A utility library for scheduling periodic and non-periodic jobs efficiently. Refer to https://github.com/hypertrack/smart-scheduler-android for more info

Piyush Gupta

December 13, 2016
Tweet

Other Decks in Technology

Transcript

  1. Agenda • Android Scheduling • Android Scheduling APIs • HyperTrack’s

    need for scheduling • Smart-Scheduler Library • Future Work • Questions
  2. 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.
  3. 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
  4. 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.
  5. 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
  6. 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
  7. 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
  8. 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.
  9. 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.