Java Android 工作管理器与服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50343578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Work Manager vs Services?
提问by Sushobh Nadiger
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 Managera 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?
在我的 Android 应用程序中,我有多个意图服务,它们一个接一个地运行,第一个意图服务由广播触发。几天前我遇到了Work Manager,非常喜欢 Worker 和 WorkManager 类的简单性。与常规意图服务相比,工作管理器的优缺点是什么?考虑到我将来可能必须编写更多意图服务,我现在应该切换到工作经理吗?还有哪个选项可以帮助我轻松测试代码?
回答by Sagar
WorkManager comes with following features:
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.
- 提供可以在进程死亡中存活的任务
- 它可以唤醒应用程序和应用程序的进程来完成工作,从而保证工作将被执行。
- 允许观察工作状态和创建复杂工作链的能力
- 允许工作链,允许将大块工作分成小工作并根据不同的约束执行它们
- 优雅地管理打盹模式或操作系统施加的其他限制。
Following would be the cases where it would be helpful:
以下是它会有所帮助的情况:
- Executing long running background tasks like uploading media
- Parsing and storing data in database.
- Critical Tasks which needs to survive process deaths
- 执行长时间运行的后台任务,如上传媒体
- 在数据库中解析和存储数据。
- 需要在过程死亡中幸存的关键任务
Should i switch to work manager now considering the fact that i may have to write more intent services in the future?
考虑到我将来可能需要编写更多意图服务,我现在应该切换到工作经理吗?
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.
在大多数情况下,它应该是 IntentService 的替代品,但您必须在使用前仔细考虑。可能 IntentService 一开始就不是最佳选择。
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 不是对所有后台任务的回答。EG 你不应该用它来处理付款,因为它不需要在进程死亡后幸存下来,而且这些任务需要立即执行。考虑使用前台服务。使用它们来解析视图的数据和内容也不是一个好主意。
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 将是我们后台处理问题的解决方案。当然,这将是最重要的选择,因为它抽象了操作系统强加的几个约束的处理。您应该考虑将其用于未来的实现。
Also which option would help me test the code easily?
还有哪个选项可以帮助我轻松测试代码?
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.
Google 还提供了测试库,方便 WorkManager 的测试。它仍在开发中,但在发布之前应该会变得更加强大。