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)?
Both work same but the only difference with JobIntentService is that JobIntentService gets restarted if the application gets killed while the service was executing.
You can say JobIntentService is a modern way to run the background service from the background application. JobIntentService. JobIntentService works in the same way as a Service however it enqueues the work into the JobScheduler on compatible Android targets( SDK 26 or more).
This class is deprecated. This class has been deprecated in favor of the Android Jetpack WorkManagerlibrary, which makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts.
JobIntentService sample app Just Open the Android Studio, Go to File menu and create a new project, fill the project name (I'm using JobIntentService Example) and select the EmptyActivity template. Now create a new file with is MyJobIntentService which extends the JobIntentServcie.
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.
JobService is used to schedule background work with JobScheduler. The above code snippet for ExampleJobService.class
can be used to start a JobService.
Where as, a JobIntentService can be started using below code:
// JobIntentService for background task Intent i = new Intent(context, ExampleJobIntentService.class); ExampleJobIntentService.enqueueWork(context,i);
The JobIntentService is capable to work for both before and after Android Oreo devices.
When running on older than Oreo versions of the platform, JobIntentService will use Context.startService. When running on Android O or later, the work will be dispatched as a job via JobScheduler.enqueue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With