IntentService + startForeground与JobIntentService [英] IntentService + startForeground vs JobIntentService

查看:196
本文介绍了IntentService + startForeground与JobIntentService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Android Oreo后台执行限制,因此文档建议将IntentService重构为JobIntentService.

https://developer.android.com/about/versions/oreo/background

JobIntentService作为Oreo下面的IntentService立即运行,但在Oreo +上计划作业

https://developer.android.com/reference/android/support/v4/app/JobIntentService

在什么情况下,将正常的IntentService用作具有持久性通知的前台Service,什么时候更有意义?

JobIntentService中我可以看到的一个缺点是它不会立即开始.

解决方案

前台服务不受Doze的影响,但是如果需要在屏幕关闭时继续执行任务,则仍然必须使用唤醒锁.

JobIntentService(使用JobScheduler)为您管理唤醒锁,但是您对何时开始作业的控制较少.

我将前台IntentService(或Service)用于应立即运行且不应被系统暂停/杀死的高优先级任务(例如下载数据库).

我将JobIntentService与AlarmManager结合使用来安排低优先级任务,例如定期刷新窗口小部件的数据.

Since Android Oreo background execution limits, the docs recommend to refactor IntentServices to JobIntentService.

https://developer.android.com/about/versions/oreo/background

JobIntentService runs immediately as an IntentService below Oreo, but schedules a Job on Oreo+

https://developer.android.com/reference/android/support/v4/app/JobIntentService

In what cases would it make sense to run a normal IntentService as a foreground Service with a persistent notification, and when is a JobIntentService better?

One downside I can see in JobIntentService is that it doesn't start immediately.

解决方案

Foreground service is not affected by Doze, but you still have to use wake locks, if you need your task to be continued when the screen is off.

The JobIntentService (which uses the JobScheduler) manages wake locks for you, but you have less control when the job will be started.

I would use the foreground IntentService (or Service) for high priority tasks (e.g. downloading a database) that should run immediatelly and that should not be paused / killed by system.

I would use the JobIntentService in conjunction with AlarmManager to schedule low priority tasks like refreshing the widget's data periodically.

这篇关于IntentService + startForeground与JobIntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆