Android Work Manager与服务? [英] Android Work Manager vs Services?

查看:155
本文介绍了Android Work Manager与服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,我有多个意图服务,这些服务一个接一个地运行,并且第一个意图服务由广播进行攻击。我几天前遇到了工作经理,真的很喜欢工人的简单性和WorkManager类。工作管理器对常规意图服务的利弊是什么?我现在应该转换到工作经理,考虑到将来我可能不得不写更多的意向服务吗?另外哪个选项可以帮助我轻松测试代码?

In my Android app i have multiple intent services which run one after another and the very first intent service is trigerred by a broadcast. I came across Work Manager a few days ago and really liked the simplicity of the Worker and the WorkManager classes. What are the pros and cons of Work Manager over regular intent services? Should i switch to work manager now considering the fact that i may have to write more intent services in the future? Also which option would help me test the code easily?

推荐答案

WorkManager具有以下功能:


  • 提供可以在过程中死亡的任务

  • 它可以唤醒应用程序和应用程序的进程因此,工作保证了工作的执行。

  • 允许观察工作状态和创建复杂工作链的能力

  • 允许工作链接

  • 优雅地管理打盹模式或操作系统施加的其他限制。

  • Provides tasks which can survive process death
  • It can waken up the app and app's process to do the work thereby guarantees that works will be executed.
  • Allows observation of work status and the ability to create complex chains of work
  • Allows work chaining which allows to segregate big chunk of work into small works and execute them based on different constraints
  • Gracefully manages doze mode or other restrictions imposed by OS.

以下是有用的情况:


  • 执行长时间运行的后台任务,如上传媒体

  • 在数据库中解析和存储数据。

  • 需要在流程死亡中幸存的关键任务


我现在应该切换到工作经理,考虑到我可能需要在未来?

在大多数情况下,它应该是IntentService的替代品,但在使用之前必须仔细考虑。可能是因为IntentService不是最好的选择。

In most cases it should be replacement for IntentService but you have to consider carefully before using it. It could be that IntentService was not the best choice at first place.

WorkManager无法应对所有后台任务。例如。您不应该使用它来处理付款,因为它不需要在过程死亡中存活,并且这些任务需要立即执行。考虑使用前台服务。使用它们来解析数据和视图内容也不是一个好主意。

WorkManager is not answer to all of the background tasks. E.G. You shouldn't use it for processing payments since it doesn't need to survive process death and these task needs to be executed immediately. Consider using Foreground Service. Its also not a great idea to use them for parsing data and contents of view.

在使用它之前,你真的需要权衡它是否需要它的功能。由于谷歌几乎正在改变我们编码的方式,因此WorkManager将成为我们背景处理问题的解决方案。当然,它是最重要的选择,因为它抽象了操作系统强加的几个约束的处理。你应该考虑将它用于将来的实现。

You really need to weigh whether you need capabilities of it before using it. Since Google is almost re-vamping the way we code, WorkManager would be solution of our Background processing woes. For sure it would be most important option since it abstracts handling of several constraints imposed by OS. You should consider using it for future implementations.


哪个选项可以帮助我轻松测试代码?

Google还提供了测试库,方便了WorkManager的测试。它仍在开发中,但在发布之前应该变得更加强大。

Google has also provided testing library which facilitates WorkManager's test at ease. Its still under development but should become more powerful before its released.

这篇关于Android Work Manager与服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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