JobIntentService如何与JobService相关?

jus*_*oob 16 android android-jobscheduler

如果ServiceIntentService主要差异是Service在主线程上运行而IntentService不是,并且后者在工作完成时完成,而我们必须调用stopService()stopSelf()停止a Service.

这两个都可以简单地传递给startService().

怎么样JobServiceJobIntentService

我们来看下面的代码片段:

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

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

scheduler.schedule(job);
Run Code Online (Sandbox Code Playgroud)

可以ExampleJobService.class参考a JobService和a JobIntentService

将行为相同,与ServiceIntentService(除了JobScheduler可能不会立即开始工作)?

cta*_*ate 10

JobIntentService本质上是一个替代品IntentService,以与Android O的新背景执行限制"玩得很好"的方式提供类似的语义.它被实现为O +上的预定作业,但是它被抽象出来了 - 您的应用程序不需要关心它是一项工作.

从未schedule()通过JobIntentService支持类直接使用您希望使用的工作. JobIntentService使用该enqueue()系统,作业调度,你不能混搭enqueue()schedule()同样的工作.

  • **亚历山大·法伯**,根据[doc](https://developer.android.com/reference/android/support/v4/app/JobIntentService.html#onHandleWork%28android.content.Intent%29)`onHandleWork`在后台线程上运行 (2认同)

小智 8

JobService用于安排JobScheduler的后台工作.上面的代码片段ExampleJobService.class可用于启动JobService.

在哪里,可以使用以下代码启动JobIntentService:

// JobIntentService for background task
Intent i = new Intent(context, ExampleJobIntentService.class);
ExampleJobIntentService.enqueueWork(context,i);
Run Code Online (Sandbox Code Playgroud)

JobIntentService能够在Android Oreo设备之前和之后工作.

在平台的Oreo版本上运行时,JobIntentService将使用Context.startService.在Android O或更高版本上运行时,工作将通过JobScheduler.enqueue作为作业分派.

  • 我知道我们可以在运行 JobService 时为 JoScheduler 传递时间间隔,但是我如何使用 JobIntentService 做同样的事情? (3认同)