Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Background Task with WorkManager

Background Task with 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

Nsikak Thompson

November 24, 2018
Tweet

More Decks by Nsikak Thompson

Other Decks in Programming

Transcript

  1. About Me Location • I build for Android • Co-organiser

    GDG Uyo • ALC Program Assistant for South South Nigeria
  2. Scope • What is WorkManager? • Set up • Creating

    a Worker • Enqueuing WorkRequest • Chain Work • Observing your work • Cancel Work
  3. What you may have used: • AsynTask(Short duration task) •

    Firebase JobSchedulars(long background task) • AlarmManager(periodic task)
  4. 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.
  5. 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)
  6. 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)
  7. 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.
  8. 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)
  9. 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
  10. Cancel work /** * Cancel work using the work's unique

    name */ void cancelWork() { mWorkManager.cancelUniqueWork(IMAGE_MANIPULATION_WORK_NAME); }
  11. 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