Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Use Android Work Manager?

  • Last Updated : 19 Sep, 2021

We’ve taken it a step further in WorkManager 2.6 by adding support for Workers to operate in any process and allowing workers to be tied to a specific process. Multi-process functionality is very beneficial for programs that require Workers to operate in different processes. The majority of apps operate nicely in a single process. Those that require more than one process, on the other hand, have a difficult time coordinating work between processes. The WorkManager API makes it simple to plan deferrable, asynchronous activities that must be completed consistently. These APIs allow you to construct a task and provide it to WorkManager to execute when the work limits are fulfilled.

Define the Task

The Worker class is used to define work. The doWork() function executes asynchronously on a WorkManager-provided background thread.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

What’s New in V2.6?

Starting with WorkManager 2.6, you may use RemoteListenableWorker or RemoteCoroutineWorker to tie a worker to a particular process. RemoteCoroutineWorker should be used for workers written in Kotlin. Otherwise, RemoteListenableWorker should be used. In this post, we’ll show how to create a Worker in Kotlin, however for a similar Java implementation,

See the example linked below:



A partialWorkMonitor can be constructed in the same way that a CoroutineWorker can. Instead of overriding doWork, you override doRemoteWork and then bind to a specific process using two extra arguments supplied as part of the input data when constructing the work request: ARGUMENT PACKAGE NAME and ARGUMENT CLASS NAME:

Kotlin




val PACKAGE_NAME = "com.geeksforgeeks.android
val primaryName = CourtineServerBackend::class.java.name
val tertiaryName = ComponentName(PACKAGE_NAME, primaryName)
val data: Data = Data.Builder()
   .putString(NAME_OF_FILE, someName.packageName)
   .putString(NAME_OF_FILE, someName.className)
   .build()
return OneTimeWorkRequestBuilder<ExampleRemoteCoroutineWorker>
   .setInputData(data)
   .build()

Further, you must additionally provide the service definition in the AndroidManifest for each RemoteWorkerService, as follows:

<manifest ... >
    <service
            android:name="geeksforgeeks.android.spandansaxena.anshu"
            android:exported="true"
            android:process=":someRandomWorker" />

    <service
            android:name=".RemoteWorkerService2"
            android:exported="false"
            android:process=":anotherRandomWorker" />
    ...
</manifest>

Conclusion

Previously, if ActivityManager was unable to instantiate the JobService to begin a job, the job would be dropped quietly due to an underlying platform issue. When an application instance is created, WorkManager now guarantees that there is a backing scheduler task for every single WorkRequest by reconciling WorkRequest objects with jobs.

My Personal Notes arrow_drop_up

Start Your Coding Journey Now!