jus*_*oob 16 android android-jobscheduler
如果Service和IntentService主要差异是Service在主线程上运行而IntentService不是,并且后者在工作完成时完成,而我们必须调用stopService()或stopSelf()停止a 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);
Run Code Online (Sandbox Code Playgroud)
可以ExampleJobService.class参考a JobService和a JobIntentService?
将行为相同,与Service和IntentService(除了JobScheduler可能不会立即开始工作)?
cta*_*ate 10
JobIntentService本质上是一个替代品IntentService,以与Android O的新背景执行限制"玩得很好"的方式提供类似的语义.它被实现为O +上的预定作业,但是它被抽象出来了 - 您的应用程序不需要关心它是一项工作.
从未schedule()通过JobIntentService支持类直接使用您希望使用的工作. JobIntentService使用该enqueue()系统,作业调度,你不能混搭enqueue()和schedule()同样的工作.
小智 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作为作业分派.
| 归档时间: |
|
| 查看次数: |
5886 次 |
| 最近记录: |