如何注入Workerclass?

How to inject Worker class?

因此,首先我创建了名为 UpdaterWorker 的工作器 class。 之后,我在我的应用程序 class 中覆盖了 getWorkManagerConfiguration。 我创建了一个 WorkerFactory class.

这是我要做的全部,还是我错过了什么?

如何创建一次性请求?

当我尝试将 updater worker 注入 main activity 时,我收到此错误:Dagger 不支持注入 @AssistedInject 类型,.workmanager.UpdaterWorker。您的意思是改为注入它的辅助工厂类型吗?

**Updater worker**

@HiltWorker
class UpdaterWorker @AssistedInject constructor(
    private val api: ReservationApi,
    private val updaterUrl: String,
    private val prefManager: PrefManager,
    @Assisted private val context: Context,
    @Assisted workerParams: WorkerParameters
) : Worker(context, workerParams) {
...
}

** Worker Factory**
class WorkerFactory @Inject constructor(
    private val api: ReservationApi,
    @param:Named("update_url") private val updaterUrl: String,
    private val prefManager: PrefManager,
) : WorkerFactory() {

    override fun createWorker(
        appContext: Context,
        workerClassName: String,
        workerParameters: WorkerParameters
    ): ListenableWorker? {
        return when (workerClassName) {
            WorkerEnums.UpdaterWorker.name -> {
                UpdaterWorker(api, updaterUrl, prefManager, appContext, workerParameters)
            }
            else ->
                throw Resources.NotFoundException("Worker not found")
        }
    }
}
** My application class**
    override fun getWorkManagerConfiguration(): Configuration {
        return Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .build()
    }

**Main Activity**
    @Inject
    lateinit var updaterWorker: UpdaterWorker

不需要 WorkerFactory,此外,您不需要(也不能)将 Worker 注入任何其他 class。这是正确的方法:

工人

@HiltWorker
class UpdaterWorker @AssistedInject constructor(
    private val api: ReservationApi,
    private val updaterUrl: String,
    private val prefManager: PrefManager,
    @Assisted private val context: Context,
    @Assisted workerParams: WorkerParameters
) : Worker(context, workerParams) {
...
}

Worker 的使用(例如在视图模型中)

class MyTestViewModelForWorker(@ApplicationContext private val context: Context) : ViewModel() {
     private val myTestWork = OneTimeWorkRequestBuilder<UpdateWorker>
        .build()

    fun startManager() {
        WorkManager.getInstance(context).enqueue(myTestWork)
    }

}

然后,在您的应用中使用 hiltfactory 覆盖默认的 workerfactory 很重要 class

覆盖默认的 WorkerFactory

@HiltAndroidApp
class App : Application(), Configuration.Provider {
    @Inject lateinit var workerFactory: HiltWorkerFactory

    override fun getWorkManagerConfiguration(): Configuration =
        Configuration.Builder().setWorkerFactory(workerFactory).build()
}

最后,您需要删除清单中的默认 workmanagerinitalizator:

AppManifest

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

下次,请阅读 hilt 的文档,因为我在这里写的所有内容都在那里得到了完美的解释,其次,我在这里展示的方法将随着 androidx.work:work-runtime-ktx:2.6.0[=15= 的发布而改变]