如何将几个argumetns作为输入发送到WorkManager?或如何创建构造函数?或如何传递对象作为输入?

时间:2019-04-26 13:51:33

标签: android android-workmanager

我需要传递几个参数作为WorkManager的输入,但是不知道如何。另外,我想通过存储库。

    val data = workDataOf("cabinId" to task.cabinId)
     val data2 = workDataOf("repository" to repository)
     val uploadWorkRequest = OneTimeWorkRequestBuilder<WManager>()
         .setInputData(data)
         .setInputData(data2)
         .build()


class WManager(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
override fun doWork(): Result {
    val input = inputData.getInt("cabinId", 99)
    val input2 = inputData("repository")
    return Result.success(outputData)
}}

我试图为存储库实现可序列化的接口,但是它不起作用

1 个答案:

答案 0 :(得分:1)

您在做正确的事。您可以将多个键值对传递给workDataOf

类似这样:

val data = workDataOf("cabinId" to task.cabinId, "repository" to repository)
val request = OneTimeWorkRequestBuilder<WManager>()
    .setInputData(data)
    .build()

WorkManager.getContext(context).enqueue(request)

这是假设repository是可序列化的类型。如果没有,则需要进行依赖注入。