Android WorkManager Pass Parameter (Kotlin)

June 11, 2019
class DownloadPlaceWorker(context : Context, params : WorkerParameters) : Worker(context, params) {
    companion object {
        private const val WORK_NAME = "DownloadPlaceWorker"
        private const val PARAM_PARENT_KEY = "parent_key"

        fun run(parentKey: String) : LiveData<WorkInfo> {

            val data = workDataOf(PARAM_PARENT_KEY to parentKey)
            val work = OneTimeWorkRequestBuilder<DownloadPlaceWorker>()
                    .setInputData(data)
                    .build()
            WorkManager.getInstance().enqueueUniqueWork(WORK_NAME, ExistingWorkPolicy.KEEP, work)

            return WorkManager.getInstance().getWorkInfoByIdLiveData(work.id)
        }
    }

    override fun doWork(): Result {
        val parentKey = inputData.getString(PARAM_PARENT_KEY)


        return Result.success()
    }
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.