4

I am still new to android and working on an application that works on Android (API >= 21). This application displays data that is previously downloaded from a server on the local network so I need to implement a service to download the content, on-demand & Periodically, on the device.

SyncAdapter

I've implemented this "Downloader" using SyncAdapter and it was working fine but at the end the code was really verbose:

  • The application does not have ContentProvider. The content is downloaded as files.
  • It runs on a local closed network so there is no need for authentication.

The application had 3/4 extra classes that not doing any real job.

JobScheduler

After some reading and searching, I decided to go with JobScheduler.

"JobScheduler is guaranteed to get your job done." medium article

It looks easy and has clear API, I said, so I re-implemented my "Downloader" with JobScheduler and the result was really good. The application was able to download the content, writing a good log to trace errors and operations. The Job runs when the device is Idle and kicks off/stopped, on demand, as expected.

I used alarm manager to implement the periodical calls of the job and turning wifi on. Oh, I forgot to mention that the application is the responsible of turning on the wifi because it is inside a case and works as Kiosk

The problem is that there is a catch. This is not mentioned in the documentation, or I was blind not to see it, at all. The Job is limited to 1 minute only (1 minute on lolipop and more on Android >= 0) then the onStopJob() will be called. So my is cancelled before completing the download when the data is a little big. Of course I can keep my thread going and continue download in the background but in this case I can't benefit from the API to maintain a good log, reschedule my job and manage Wifi status.

I need another implementation

I need something similar to SyncAdapter or JobScheduler that runs when the Wifi is on and when the device is Idle. I am not sure whether triggering a service from JobScheduler is the solution I am left with. I need a little certain information before implementing the same thing for the third time.

Any idea?

Odai Alali
  • 186
  • 1
  • 8

0 Answers0