IntentService + startForeground与JobIntentService

Flo*_*her 5 android android-service intentservice android-intentservice android-jobscheduler

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

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

JobIntentService立即以IntentService低于Oreo的价格运行,但在Oreo +上安排作业

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

在什么情况下,将法线IntentService作为Service具有持久性通知的前台会有意义,什么时候JobIntentService更好?

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

Der*_*k K 9

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

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

我会将前台 IntentService(或服务)用于高优先级任务(例如下载数据库),这些任务应该立即运行并且不应被系统暂停/杀死。

我会结合使用 JobIntentService 和 AlarmManager 来安排低优先级任务,例如定期刷新小部件的数据。