Android O background services effect on REST Client App

May 17, 2017 15:01 · 287 words · 2 minute read

When creating an Andrioid REST Client app i usually have a Android Service that consume the REST API and Store it on a Content Provider, this technique is coming from an Presentation on 2010 Google IO (see the video below)

slides for the presentation https://dl.google.com/googleio/2010/android-developing-RESTful-android-apps.pdf

On that presentation it say to there are 3 basic pattern for a REST client

  1. Use Service API
  • Use Content Provider API
  • Use Content Provider API & Sync Adapter

Since on Android O if we call a Service via startService() it will throw a IllegalArgumentException which mean we cannot use the Service by use startService.

Because of Service limitation the Android Team suggest to switch using JobService that introduced on android M (for backward compatibility we can use FirebaseJobDispatcher or Evernote JobService ) the JobService basically a service that is run based on certain condition (Network Type, Power Type, Duration, etc) and it cannot run directly it must make some calculation and scheduled before running, it will have some delay and the delay between requesting for running cannot be determined (it depend on Android/Google Play)

Based on the basic patterns for REST client above and the Service limitation it seems the only option to perform REST API calling from background and stored it safely is by using the Content Provider API & Sync Adapter

how the Content Provider API & Sync Adapter work (based on my interpresentation of the slide):

  1. Activity / Fragment call an REST API via Content Provider asynchronously (use Loader/AsyncTask/Handler/etc)
  • Content Provider is Calling a Sync Adapter to perform REST API Call
  • REST response is stored on the Content Provider
  • Content Provider notify an app using ContentObserver/Loader

a good tutorial for creating a SyncAdapter is on https://developer.android.com/training/sync-adapters/index.html and http://blog.udinic.com/2013/07/24/write-your-own-android-sync-adapter/

comments powered by Disqus