Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Worker with parameters for in WorkManager for Android?

Android architecture has a new components WorkManager.

From the example,

class CompressWorker(context : Context, params : WorkerParameters)     : Worker(context, params) {      override fun doWork(): Result {          // Do the work here--in this case, compress the stored images.         // In this example no parameters are passed; the task is         // assumed to be "compress the whole library."         myCompress()          // Indicate success or failure with your return value:         return Result.SUCCESS          // (Returning RETRY tells WorkManager to try this task again         // later; FAILURE says not to try again.)      }  }  val compressionWork = OneTimeWorkRequestBuilder<CompressWorker>().build() 

How can I create a Worker that accept parameters in constructor or doWork?

like image 947
Joshua Avatar asked Oct 04 '18 04:10

Joshua


People also ask

How do you pass parameters to WorkManager?

public static OneTimeWorkRequest create(String id) { Data inputData = new Data. Builder() . putString(TASK_ID, id) . build(); return new OneTimeWorkRequest.

How do you pass data to worker?

To pass an argument to a task, call the WorkRequest. Builder. setInputData(Data) method before you create the WorkRequest object. That method takes a Data object, which you create with Data.

Which API do you use to add constraints to a WorkRequest?

To create a set of constraints and associate it with some work, create a Constraints instance using the Contraints. Builder() and assign it to your WorkRequest. Builder() .

What is difference between WorkManager and coroutines?

It is important to note that coroutines is a concurrency framework, whereas WorkManager is a library for persistent work.


1 Answers

You can use setInputData method to send data just like Bundle.

/***  Logic to set Data while creating worker **/ val compressionWork = OneTimeWorkRequest.Builder(CompressWorker::class.java) val data = Data.Builder() //Add parameter in Data class. just like bundle. You can also add Boolean and Number in parameter. data.putString("file_path", "put_file_path_here") //Set Input Data compressionWork.setInputData(data.build()) //enque worker WorkManager.getInstance().enqueue(compressionWork.build())   /*** Logic to get Data  ***/ class CompressWorker(context : Context, params : WorkerParameters)     : Worker(context, params) {      override fun doWork(): Result {          //get Input Data back using "inputData" variable          val filePath =  inputData.getString("file_path")          // Do the work here--in this case, compress the stored images.         // In this example no parameters are passed; the task is         // assumed to be "compress the whole library."         myCompress()          // Indicate success or failure with your return value:         return Result.SUCCESS          // (Returning RETRY tells WorkManager to try this task again         // later; FAILURE says not to try again.)      }  } 

For more information visit this link.

like image 53
Dhaval Patel Avatar answered Oct 05 '22 03:10

Dhaval Patel