How is JobIntentService related to JobService?

為{幸葍}努か 提交于 2021-02-06 15:02:33

问题


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)?


回答1:


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.




回答2:


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.




回答3:


Actually, JobIntentService would be better if rename to JobedIntendService, one get obscured with JobService at first glance.



来源:https://stackoverflow.com/questions/46336977/how-is-jobintentservice-related-to-jobservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!