Scheduling Work

To schedule a Worker to execute, you need a WorkRequest. The WorkRequest class itself is abstract, so you need to use one of its subclasses depending on the type of work you need to execute. If you have something that only needs to execute once, use a OneTimeWorkRequest. If your work is something that must execute periodically, use a PeriodicWorkRequest.

For now, you are going to use the OneTimeWorkRequest. This will let you verify that your PollWorker is functioning correctly and learn more about creating and controlling the requests. Later you will update your app to use a PeriodicWorkRequest.

Open PhotoGalleryFragment.kt, create a work request, and schedule it for execution.

Listing 27.3  Scheduling ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.