$30 off During Our Annual Pro Sale. View Details »

Android JobScheduler Example

Android JobScheduler Example

Very brief slides to accompany a sample app demonstrating the Android JobScheduler. Sample code can be found at https://github.com/alphonzo79/AndroidJobSchedulerExample

Joe Rowley

May 10, 2016
Tweet

More Decks by Joe Rowley

Other Decks in Programming

Transcript

  1. JobScheduler Use Case • Periodic background data sync • Retry

    failed operation at a future time • On a given retry schedule • Perform non-urgent work when certain preconditions are met • Downloading supplemental data
  2. Other Ways • Service with a Thread.sleep() or with a

    Handler.postDelayed() • Requires the app to continue running during the interim — Resources • If the app crashes or the user kills it, we lose our task • Not robust • AlarmService • Works well if you can’t use JobScheduler (< 21) • https://github.com/evant/ JobSchedulerCompat • Non-persistent across reboot • Retries must be set up manually • Does not consider device state
  3. JobScheduler Setup • Extend JobService • This Service must require

    android.permission.BIND_JOB_SERVICE in the Manifest • Obtain JOB_SCHEDULER_SERVICE with Context.getSystemService() • Create a JobInfo.Builder, passing in the class of your JobService extension • Set options on the Builder • MinimumLatency, RequiredNetworkType, RequiresDeviceIdle, BackoffCriteria, OverrideDeadline, Periodic, Persisted, RequiresCharging, PersistableBundle extras • Schedule the job with JobScheduler.schedule(jobBuilder.build())
  4. JobService Extension • onStartJob(JobParameters params) • Runs synchronously on the

    main thread and expects to immediately return • If background work is needed, set it up to work on a background thread • Return = Am I doing stuff on a background thread? • When complete (successfully or on error), call jobFinished(JobParameters params, boolean needToReschedule) • onStopJob(JobParameters params) • Must stop your background thread if one is running. • Return = Do I need to reschedule this work according to the retry criteria set at creation time?