Slide 1

Slide 1 text

Nsikak Thompson Abuja Background Task with WorkManager

Slide 2

Slide 2 text

About Me Location ● I build for Android ● Co-organiser GDG Uyo ● ALC Program Assistant for South South Nigeria

Slide 3

Slide 3 text

3 DevFest Uyo

Slide 4

Slide 4 text

Scope ● What is WorkManager? ● Set up ● Creating a Worker ● Enqueuing WorkRequest ● Chain Work ● Observing your work ● Cancel Work

Slide 5

Slide 5 text

What you may have used: ● AsynTask(Short duration task) ● Firebase JobSchedulars(long background task) ● AlarmManager(periodic task)

Slide 6

Slide 6 text

JobSchedular AlarmManager WorkManager

Slide 7

Slide 7 text

What is WorkManager WorkManager is part of Android Jetpack and an Architecture Component for background work that needs a combination of opportunistic and guaranteed execution.

Slide 8

Slide 8 text

What is WorkManager WorkManager is a simple, but incredibly flexible library that has many additional benefits. These include: ● Support for both asynchronous one-off and periodic tasks ● Support for constraints such as network conditions, storage space, and charging status ● Chaining of complex work requests, including running work in parallel ● Output from one work request used as input for the next ● Handles API level compatibility back to API level 14(see note)

Slide 9

Slide 9 text

What is WorkManager ● Works with or without Google Play services ● Follows system health best practices ● LiveData support to easily display work request state in UI(WorkInfo)

Slide 10

Slide 10 text

..build.gradle file Dependencies{ //workmanager }

Slide 11

Slide 11 text

11 Profile Photo Upload

Slide 12

Slide 12 text

Classes you should know ● Worker: This is where you put the code for the actual work you want to perform in the background. You'll extend this class and override the doWork() method. ● WorkRequest: This represents a request to do some work. You'll pass in your Worker as part of creating your WorkRequest. When making the WorkRequest you can also specify things like Constraints on when the Worker should run. ● WorkManager: This class actually schedules your WorkRequest and makes it run.

Slide 13

Slide 13 text

Ready to Cook

Slide 14

Slide 14 text

Your worker class UploadImageWorker(var context:Context,var workerParams: WorkerParameters): Worker(context,workerParams){ override fun doWork(): Result { UploadImage() //your logic }

Slide 15

Slide 15 text

Enqueuing WorkRequest ● OneTimeWorkRequest ● PeriodicWorkRequest var uploadImageRequest = OneTimeWorkRequest.Builder(UploadImageWorker::class.java) .setInputData(createInputDataForUri()) .addTag(“image Upload”) .setConstraints(//like when device is charging) .build() mWorkManager.enqueue(uploadImageRequest)

Slide 16

Slide 16 text

Chain Work WorkerRequests that run in order or parallel. WR A Output WR B

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Observing your work This section uses LiveData heavily. LiveData is an observable, lifecycle-aware data holder. getStatusByIdLiveData - Get work using request id getStatusesForUniqueWorkLiveData - Get work using unique chain name getStatusesByTagLiveData - Get work using request tag

Slide 19

Slide 19 text

Observing your work Var status = mWorkManager.getWorkInfoByIdLiveData(uploadImageRequest.id): LiveData WorkInfo.State{ ENQUEUEING RUNNING SUCCEEDED FAILED BLOCKED CANCEL }

Slide 20

Slide 20 text

Cancel work With WorkManager, you can cancel work using the id, by tag and by unique chain name.

Slide 21

Slide 21 text

Cancel work /** * Cancel work using the work's unique name */ void cancelWork() { mWorkManager.cancelUniqueWork(IMAGE_MANIPULATION_WORK_NAME); }

Slide 22

Slide 22 text

Recap Congratulations! ● Adding WorkManager to your Project ● Scheduling a OneOffWorkRequest ● Input and Output parameters ● Tagging WorkRequests ● Displaying WorkInfo in the UI ● Cancelling WorkRequests ● Adding constraints to a WorkRequest

Slide 23

Slide 23 text

Abuja Thank you! Nsikak Thompson @thompson_nsikak

Slide 24

Slide 24 text

Q&A Abuja