WorkManager 2.5.0 多进程 - 在指定进程中不工作 运行
WorkManager 2.5.0 mulitprocess - work not running in designated process
我正在尝试使用 androidx.work:work-multiprocess:2.5.0 来 运行 在使用 [=40= 指定的进程中工作](),但是无论我从哪个进程入队工作,doWork() 方法都会在主应用程序进程中被调用。
如 documentation 所述 RemoteWorkManager 始终与指定进程联系。进程内调度程序也在指定进程中 运行s。
我的完整测试项目在这里:https://github.com/padreMateo88/multiprocessWorkManagerTest
我使用以下依赖项:
implementation 'androidx.work:work-runtime-ktx:2.5.0'
implementation 'androidx.work:work-multiprocess:2.5.0'
我删除了清单中的默认 WorkManagerInitialiser:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.workmanagertest">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.workmanagertest.SecondProcessBroadcastReceiver"
android:process=":second_process" />
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
tools:node="remove" />
</application>
</manifest>
在应用程序 class 中实施 Configuration.Provider:
class MyApplication : Application(), Configuration.Provider {
override fun getWorkManagerConfiguration(): Configuration = WorkConfigurationProvider().get()
}
class WorkConfigurationProvider {
fun get() = Configuration.Builder().setDefaultProcessName(processName).build()
companion object {
private const val processName = "com.example.workmanagertest:second_process"
}
}
并使用 RemoteWorkManager.getInstance() 对我的工作进行排队:
class SampleWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
override fun doWork(): Result {
Util.d(applicationContext, "SampleWorker.doWork()")
return Result.success()
}
companion object {
@JvmStatic
fun enqueueWork(context: Context) {
Util.d(context,"SampleWorker.enqueueWork()")
try {
val rwm = RemoteWorkManager.getInstance(context)
Util.d(context,"RemoteWorkManager hash ${rwm.hashCode()}")
rwm.enqueueUniqueWork(
"SampleWorker",
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequest.from(SampleWorker::class.java)
)
} catch (ex: Throwable) {
Util.d(context,"SampleWorker, WorkManager is not initialized properly, reason: " + ex.message)
}
}
}
}
我做错了什么?
为了在指定进程中调用 doWork() 方法,您还需要在清单中为 RemoteWorkerManagerService 和 SystemJobService 设置指定进程名称:
<service
android:name="androidx.work.multiprocess.RemoteWorkManagerService"
tools:replace="android:process"
android:process=":second_process"/>
<service
android:name="androidx.work.impl.background.systemjob.SystemJobService"
tools:replace="android:process"
android:process=":second_process"/>
您可以在这里找到一个工作示例:
https://github.com/padreMateo88/multiprocessWorkManagerTest
此实现基于我从 Google 问题跟踪器中获得的线索:
https://issuetracker.google.com/issues/180255558
我正在尝试使用 androidx.work:work-multiprocess:2.5.0 来 运行 在使用 [=40= 指定的进程中工作](),但是无论我从哪个进程入队工作,doWork() 方法都会在主应用程序进程中被调用。
如 documentation 所述 RemoteWorkManager 始终与指定进程联系。进程内调度程序也在指定进程中 运行s。
我的完整测试项目在这里:https://github.com/padreMateo88/multiprocessWorkManagerTest
我使用以下依赖项:
implementation 'androidx.work:work-runtime-ktx:2.5.0'
implementation 'androidx.work:work-multiprocess:2.5.0'
我删除了清单中的默认 WorkManagerInitialiser:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.workmanagertest">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.workmanagertest.SecondProcessBroadcastReceiver"
android:process=":second_process" />
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
tools:node="remove" />
</application>
</manifest>
在应用程序 class 中实施 Configuration.Provider:
class MyApplication : Application(), Configuration.Provider {
override fun getWorkManagerConfiguration(): Configuration = WorkConfigurationProvider().get()
}
class WorkConfigurationProvider {
fun get() = Configuration.Builder().setDefaultProcessName(processName).build()
companion object {
private const val processName = "com.example.workmanagertest:second_process"
}
}
并使用 RemoteWorkManager.getInstance() 对我的工作进行排队:
class SampleWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
override fun doWork(): Result {
Util.d(applicationContext, "SampleWorker.doWork()")
return Result.success()
}
companion object {
@JvmStatic
fun enqueueWork(context: Context) {
Util.d(context,"SampleWorker.enqueueWork()")
try {
val rwm = RemoteWorkManager.getInstance(context)
Util.d(context,"RemoteWorkManager hash ${rwm.hashCode()}")
rwm.enqueueUniqueWork(
"SampleWorker",
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequest.from(SampleWorker::class.java)
)
} catch (ex: Throwable) {
Util.d(context,"SampleWorker, WorkManager is not initialized properly, reason: " + ex.message)
}
}
}
}
我做错了什么?
为了在指定进程中调用 doWork() 方法,您还需要在清单中为 RemoteWorkerManagerService 和 SystemJobService 设置指定进程名称:
<service
android:name="androidx.work.multiprocess.RemoteWorkManagerService"
tools:replace="android:process"
android:process=":second_process"/>
<service
android:name="androidx.work.impl.background.systemjob.SystemJobService"
tools:replace="android:process"
android:process=":second_process"/>
您可以在这里找到一个工作示例: https://github.com/padreMateo88/multiprocessWorkManagerTest
此实现基于我从 Google 问题跟踪器中获得的线索: https://issuetracker.google.com/issues/180255558