使用自定义参数实现AndroidX Worker

时间:2019-05-09 07:44:37

标签: android kotlin androidx android-workmanager

我正在从项目中的Android Priority JobQueue library迁移到AndroidX WorkManager,但是它的构建方式是Worker应该是这样的:

TaskSchedulerWorker.kt

class TaskSchedulerWorker(
    private val workManager: WorkManager = WorkManager.getInstance()
) : TaskScheduler {

    override fun execute(useCaseWrapper: UseCaseWrapper) {
        workManager.beginUniqueWork(
            useCaseWrapper.useCaseName,
            ExistingWorkPolicy.APPEND,
            OneTimeWorkRequest.from(UseCaseWrapperWorker::class.java)
        ).enqueue()
    }

    override fun onCancel(useCaseWrapper: UseCaseWrapper) {
        workManager.cancelUniqueWork(useCaseWrapper.useCaseName)
    }
}

UseCaseWrapperWorker.kt

class UseCaseWrapperWorker(
    context: Context, 
    params: WorkerParams,
    private val useCaseWrapper: UseCaseWrapper
) : Worker(context, params) {

    override fun doWork(): Result {
        return try {
            useCaseWrapper.execute()
            Result.success()
        } catch (ex: Exception) {
            ex.printStackTrace()
            Result.failure()
        }
    }
}

问题是,如果我尝试使用这些类运行我的应用,则会收到下一个错误:

2019-05-09 09:34:45.784 27260-27443/com.mobileapp.debug E/WM-WorkerFactory: Could not instantiate com.mobileapp.domain.jobqueue.UseCaseWrapperWorker
    java.lang.NoSuchMethodException: com.mobileapp.domain.jobqueue.UseCaseWrapperWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
        at java.lang.Class.getConstructor0(Class.java:2328)
        at java.lang.Class.getDeclaredConstructor(Class.java:2167)
        at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:92)
        at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:234)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:128)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
2019-05-09 09:34:45.784 27260-27443/com.mobileapp.debug E/WM-WorkerWrapper: Could not create Worker com.mobileapp.domain.jobqueue.UseCaseWrapperWorker

因为Worker构造函数不能具有两个以上的默认参数(ContextWorkerParameters)。

我无法弄清楚如何使用构造函数来实现UseCaseWrapperWorker类,该构造函数接收ContextWorkerParametersUseCaseWrapper并从中调用TaskSchedulerWorkerOneTimeWorkRequest.from(UseCaseWrapperWorker::class.java)指令。


已编辑

我试图实现一个CustomWorkerFactory和一个CustomWorker来将它们传递给UseCaseWrapper。问题是我必须在WorkManager方法(Android Developer Documentation)中初始化Application#onCreate,并且此时我无法访问所需的UseCaseWrapper。 / p>

TaskSchedulerWorker.kt

WorkManager的初始化应该在Application#onCreate中)

class TaskSchedulerWorker(private val appContext: Context) : TaskScheduler {

    override fun execute(useCaseWrapper: UseCaseWrapper) {
        val workerFactory = CustomWorkerFactory(useCaseWrapper)
        val configuration = Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .build()
        WorkManager.initialize(context, configuration)

        WorkManager.getInstance().beginWork(
            "uniqueWorkName",
            ExistingWorkPolicy.APPEND,
            OneTimeWorkRequest.from(CustomWorker::class.java)
        )
    }
}

CustomWorkerFactory.kt

class CustomWorkerFactory(private val useCaseWrapper: UseCaseWrapper) : WorkerFactory() {

    override fun createWorker(
        appContext: Context,
        workerClassName: String,
        workerParameters: WorkerParameters
    ): ListenableWorker? {
        return CustomWorker(appContext, workerParameters, useCaseWrapper)
    }
}

CustomWorker.kt

class CustomWorker(
    context: Context, 
    params: WorkerParameters
) : Worker(context, params) {

    private var useCaseWrapper: UseCaseWrapper? = null

    constructor(
        context: Context,
        params: WorkerParameters,
        useCaseWrapper: UseCaseWrapper
    ) : this(context, params) {
        this.useCaseWrapper = useCaseWrapper
    }

    override fun doWork(): Result {
        return useCaseWrapper?.let { useCaseWrapper ->
            try {
                useCaseWrapper.execute()
                Result.success()
            } catch (ex: Exception) {
                Result.failure()
            }
        } ?: Result.failure()
    }
}

抛出的RuntimeException是:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobileapp.debug/com.mobileapp.home.view.activity.HomeActivity}: java.lang.IllegalStateException: WorkManager is already initialized.  
Did you try to initialize it manually without disabling WorkManagerInitializer? See WorkManager#initialize(Context, Configuration) or the class levelJavadoc for more information.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6981)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: java.lang.IllegalStateException: WorkManager is already initialized.  Did you try to initialize it manually without disabling WorkManagerInitializer? 
See WorkManager#initialize(Context, Configuration) or the class levelJavadoc for more information.
    at androidx.work.impl.WorkManagerImpl.initialize(WorkManagerImpl.java:137)
    at androidx.work.WorkManager.initialize(WorkManager.java:169)
    at com.mobileapp.domain.worker.TaskSchedulerWorker.execute(TaskSchedulerWorker.kt:20)
    at com.mobileapp.domain.usecase.UseCaseHandler.execute(UseCaseHandler.kt:41)
    at com.mobileapp.domain.usecase.UseCaseCall.execute(UseCaseCall.kt:50)
    at com.mobileapp.home.view.presenter.HomePresenter.getConfigurationJSON(HomePresenter.kt:107)
    at com.mobileapp.home.view.activity.HomeActivity.initComponents(HomeActivity.kt:107)
    at com.mobileapp.home.view.activity.HomeActivity.onCreate(HomeActivity.kt:67)
    at android.app.Activity.performCreate(Activity.java:7326)
    at android.app.Activity.performCreate(Activity.java:7317)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3066)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:6981) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445) 

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您应该使用自定义的WorkerFactory来初始化您的Workers。参见https://developer.android.com/reference/androidx/work/WorkerFactory

答案 1 :(得分:0)

您需要禁用默认的初始化程序

  

如果要控制初始化过程,则必须禁用   默认的初始化程序,然后定义自己的自定义配置。

     

禁用默认的初始化程序

     

要提供您自己的配置,   您必须首先删除默认的初始化程序。   为此,请使用合并规则更新AndroidManifest.xml   工具:node =“删除”:

<provider
        android:name="androidx.work.impl.WorkManagerInitializer"
        android:authorities="${applicationId}.workmanager-init"
        tools:node="remove" />

https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration