在Service和IntentService的情况下,主要的区别是Service在主线程上运行,而IntentService不在主线程上运行,后者在工作完成时自动完成,而我们必须调用stopService()或stopSelf()来停止Service。
这两个都可以简单地传递给startService()。
那么JobService和JobIntentService呢?
让我们看一下下面的代码片段:
JobInfo job = new JobInfo.Builder(id, new ComponentName(context, ExampleJobService.class))
.build();
JobScheduler scheduler = (JobScheduler) context
.getSystemService(Context.JOB_SCHEDULER_SERVICE);
scheduler.schedule(job);ExampleJobService.class可以同时引用JobService和JobIntentService吗
行为是否与Service和IntentService相同(除了JobScheduler可能不会立即启动作业)?
发布于 2017-09-29 04:37:37
JobIntentService本质上是IntentService的替代品,以一种与Android O新的后台执行限制“很好地配合”的方式提供了类似的语义。它是作为O+上的调度作业实现的,但这是抽象的--您的应用程序不需要关心它是一个作业。
永远不要直接支持您希望通过JobIntentService schedule()类使用的作业。JobIntentService在作业调度程序中使用enqueue()系统,对于同一作业,不能混用enqueue()和schedule()。
发布于 2018-04-27 19:46:03
JobService用于安排JobScheduler的后台工作。上面的ExampleJobService.class代码片段可用于启动JobService。
其中,as可以使用以下代码启动JobIntentService:
// JobIntentService for background task
Intent i = new Intent(context, ExampleJobIntentService.class);
ExampleJobIntentService.enqueueWork(context,i);JobIntentService可以在Android Oreo之前和之后的设备上运行。
当在比Oreo版本更早的平台上运行时,JobIntentService将使用Context.startService。当在Android或更高版本上运行时,工作将通过JobScheduler.enqueue作为作业分派。
https://stackoverflow.com/questions/46336977
复制相似问题