From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Send progress updates from a background worker
From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
Send progress updates from a background worker
- [Instructor] As I showed previously, the WorkManager API lets you send messages from the Worker to the UI. In addition to sending final results when it's successful, you can also report progress periodically. The syntax for this is pretty much the same as sending data but you have to change the Worker class's superclass so that you're using a Coroutine. I'll change the superclass for MyWorker class, from Worker to CoroutineWorker. Now, I don't need to set up a scope that's done automatically. But because I'm now working within a Coroutine, the dowork function has to be suspending, so I'll add the suspend keyword right there. Next, I'll add some code so that I'm mimicking a long running function. In order to set a progress update, call the function setProgress and then pass in a data object. Just like before, I'm going to be using the workDataOf function and sending a pair with a key and a value. So I'm going to…
Contents
-
-
-
-
-
-
(Locked)
Run background tasks with IntentService5m 22s
-
(Locked)
Upgrade IntentService to JobIntentService4m 37s
-
(Locked)
Send results from JobIntentService to the UI7m 25s
-
(Locked)
Manage deferrable tasks with WorkManager5m 35s
-
(Locked)
Set work request constraints3m 9s
-
(Locked)
Get results from a background worker4m 56s
-
(Locked)
Send progress updates from a background worker5m 2s
-
(Locked)
-
-