问题描述
我有一个后台任务,必须在固定过程中运行.我目前正在为此使用 JobIntentService
.但是,我想迁移到WorkManager来利用基于网络的任务延迟.
I have a background task that has to run in a fixed process. I'm currently using a JobIntentService
for this. However, I'd like to migrate to WorkManager to make use of network based task delays.
是否使用WorkManager或以任何其他API 14-28兼容方式解决此问题?
Is there any solution to this either using WorkManager or in any other API 14-28 compatible way?
推荐答案
WorkManager 2.5.0中的新增功能,提供了一个workmanager-multiprocess模块来解决此问题.您可以使用实现"androidx.work:work-multiprocess:$work_version"
New in WorkManager 2.5.0, there is a workmanager-multiprocess module to solve this problem. You can implement this withimplementation "androidx.work:work-multiprocess:$work_version"
要使用此功能,可以注册一个新的 WorkManager.Initializer
,如本页中所述: https://developer.android.com/topic/libraries/架构/工作管理器/高级/自定义配置
To use this, you can register a new WorkManager.Initializer
as detailed in this page:https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration
注册此初始化程序时,您的配置需要包括对 .setDefaultProcessName("com.example.app:processName")
的调用.这将使呼叫以正确的过程进行.我现在正在对此进行测试,它似乎正在运行,但是如果有任何问题,我将更新此帖子.
When you register this initializer, your configurations need to include a call to .setDefaultProcessName("com.example.app:processName")
. This will make the calls occur in the correct process. I am testing this out right now, and it seems to be working, but I'll update this post if there are any issues.
这篇关于Android Workmanager以固定流程运行作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!