How is JobIntentService related to JobService?

justanoob picture justanoob · Sep 21, 2017 · Viewed 8k times · Source

In case of Service and IntentService the main differences are Service runs on the main thread while IntentService is not, and the latter finishes itself when the work is done while we have to call either stopService() or stopSelf() to stop a Service.

Both of these can be simply passed to startService().

What about JobService and JobIntentService?

Let's take the following code snippet:

JobInfo job = new JobInfo.Builder(id, new ComponentName(context, ExampleJobService.class))
    .build();

JobScheduler scheduler = (JobScheduler) context
    .getSystemService(Context.JOB_SCHEDULER_SERVICE);

scheduler.schedule(job);

Can ExampleJobService.class refer to both a JobService and a JobIntentService?

Will the behaviour be the same as with Service and IntentService (apart from the JobScheduler may not start the jobs immediately)?

Answer

ctate picture ctate · Sep 28, 2017

JobIntentService is essentially a replacement for IntentService, offering similar semantics in a way that "plays nice" with Android O's new background execution restrictions. It is implemented as a scheduled job on O+, but that's abstracted away -- your app doesn't need to care that it's a job.

Never schedule() a job directly that you expect to use via the JobIntentService support class. JobIntentService uses the enqueue() system in the job scheduler, and you cannot mix and match enqueue() and schedule() for the same job.