何时使用JobIntentService和WorkManager? [英] When to use JobIntentService vs WorkManager?

查看:212
本文介绍了何时使用JobIntentService和WorkManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google最近不建议使用IntentService,因为: https://android.googlesource.com/platform/frameworks/base.git/+/6f8b09029932dfd24945202017639754b00acc2e

Google recently deprecated IntentService as of: https://android.googlesource.com/platform/frameworks/base.git/+/6f8b09029932dfd24945202017639754b00acc2e

IntentService的文档现在说:

The docs for IntentService now say:

 * @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之间有什么区别?在哪种情况下建议使用哪一个?

So what are the differences between JobIntentService and WorkManager and which one is recommended under which circumstances?

Google在此页面上甚至没有提及JobIntentService,他们只提及WorkManager: https://developer.android.com/guide/background

Google doesn't even mention JobIntentService on this page, they only mention WorkManager: https://developer.android.com/guide/background

推荐答案

自Android Oreo以来,我们无法再继续在后台运行常规服务,因为系统将:

Since Android Oreo we cannot keep normal Services running on the background anymore, because the system will:

如果应用本身在启动服务后进入后台运行,则大约在一分钟后杀死该服务

1-kill the service after around one minute if the app itself goes to the background after launching the service

2-如果在应用本身在后台运行时启动了服务,则会抛出异常

2-throw an exception if the service was launched when the app itself is in the background

IntentService只是普通服务的子类,它在后台线程上顺序执行所有工作,并在完成所有工作后停止运行.但是作为服务,它也受到上述限制的影响.

IntentService is just a subclass of the normal service, which executes all its work sequentially on a background thread and stops itself when it finishes executing all its work. But as a service, it is, as well, affected by the limitations mentioned above.

现在用于JobIntentService:

Now for the JobIntentService:

将在Oreo之前的设备上充当普通的IntentService(因为我们没有任何限制),而在Oreo +上,它将使用jobScheduler来实现与IntentService类似的行为.它会尽快开始工作,通过以下方式安排工作JobScheduler和JobScheduler可能会选择暂时推迟工作,但在低内存情况下,打do睡模式或达到时间限制时,其工作更有可能会有所不同或被中断(〜10分钟)

Will act as a normal IntentService on pre-Oreo Devices (because we don't have any limitations) and on Oreo+ will use jobScheduler instead to achieve similar behavior as IntentService. It just starts the work as soon as possible, schedules its work via JobScheduler, and JobScheduler may elect to postpone that work for a bit, but its job are more likely to be differed or interrupted in low-memory situtations ,in doze mode or when they reach a time limit(~10 minutes)

使用JobIntentService,不可能进行某些配置,例如,特别定义我们要在什么情况下开始工作(例如,设备当前正在充电或我们具有WIFI连接),但是使用workmanager,我们可以设置这些约束.

With JobIntentService, doing some configurations is impossible, like defining specifically under which circumstances we want our jobs to start (such as when the device is currently Charging or if we have WIFI connection), however with workmanager we can set these constraints.

使用WorkManager来处理具有约束的工作,或用于事务性正在进行的工作/工作,或用于将来可能在发生的工作,当您要在Android Oreo +上复制普通IntentService的行为以及可能会稍微延迟并且可能花费超过1分钟但不到10分钟的作业时,请使用JobIntentService.

Use WorkManager for jobs that have some Constraints,or for jobs/work that are transactional not ongoing, or for jobs that can happen sometime in the future, and use JobIntentService when you want to copy the behavior of the normal IntentService on Android Oreo+ and for jobs that can be slightly delayed and might take more than 1 minute but less than 10 minutes approx.

希望我回答了你的问题.

Hope I Answered Your question.

致谢

这篇关于何时使用JobIntentService和WorkManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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