Google 最近弃用了 IntentService:https://android.googlesource.com/platform/frameworks/base.git/+/6f8b09029932dfd24945202017639754b00acc2e https://android.googlesource.com/platform/frameworks/base.git/+/6f8b09029932dfd24945202017639754b00acc2e
IntentService 的文档现在说:
* @deprecated IntentService is subject to all the
* <a href="/preview/features/background.html">background execution limits</a>
* imposed with Android 8.0 (API level 26). Consider using {@link androidx.work.WorkManager}
* or {@link androidx.core.app.JobIntentService}, which uses jobs
* instead of services when running on Android 8.0 or higher.
那么JobIntentService和WorkManager有什么区别,什么情况下推荐使用哪一种呢?
Google 在此页面上甚至没有提到 JobIntentService,他们只提到了 WorkManager:https://developer.android.com/guide/background https://developer.android.com/guide/background
从 Android Oreo 开始,我们无法再让正常的服务在后台运行,因为系统会:
1-如果应用程序本身在启动服务后进入后台,则在大约一分钟后终止服务
2-如果应用程序本身在后台时启动服务,则抛出异常
IntentService 只是普通服务的子类,它在后台线程上顺序执行所有工作,并在执行完所有工作后自行停止。但作为一项服务,它也受到上述限制的影响。
现在对于 JobIntentService:
在 Oreo 之前的设备上将充当普通的 IntentService(因为我们没有任何限制),而在 Oreo+ 上将使用 jobScheduler 来实现与 IntentService 类似的行为。它只是尽快开始工作,通过安排其工作
JobScheduler,JobScheduler 可以选择postpone可以工作一段时间,但在内存不足、打瞌睡模式或达到时间限制(约 10 分钟)时,其工作更有可能被推迟或中断
使用JobIntentService,做一些配置就是不可能的,
比如具体定义我们希望工作在什么情况下启动(例如当设备当前正在充电时或我们有 WIFI 连接时),但是使用 Workmanager,我们可以can设置这些约束。
将 WorkManager 用于具有某些功能的作业约束条件,或者对于事务性的工作/工作没有进行中,或者可能发生的工作将来的某个时候,当您想要复制 Android Oreo+ 上正常 IntentService 的行为以及可能会稍微延迟并且可能需要大约 1 分钟以上但不到 10 分钟的作业时,请使用 JobIntentService。
希望我回答了你的问题。
Regards
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)