Creating a Worker

The Worker class is where you will put the logic you want to perform in the background. Once your worker is in place, you will create a WorkRequest that tells the system when you would like your work to execute.

Before you can add your worker, you first need to add the appropriate dependency in your app/build.gradle file.

Listing 27.1  Adding the WorkManager dependency (app/build.gradle)

dependencies {
    ...
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation "android.arch.work:work-runtime:1.0.1"
    ...
}

Sync the project to download the dependency.

With your new library in place, move on to creating your Worker. Create a new class called PollWorker that extends the Worker base class. ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.