Expedited Workers in Android

In this article, let's look into the world of Android background processing by implementing an Expedited Worker using Kotlin. This advanced feature of the WorkManager API ensures that your app's high-priority tasks are handled efficiently without delay.

In this article, let's look into the world of Android background processing by implementing an Expedited Worker using Kotlin. This advanced feature of the WorkManager API ensures that your app's high-priority tasks are handled efficiently without delay.

For a thorough understanding, we'll provide code examples in Kotlin along the way.

Expedited Workers

Expedited Workers, part of the WorkManager API, are designed for tasks that require immediate attention but aren't urgent enough for a foreground service. These workers are a extremely helpful for tasks such as data syncing, sending logs, or updating widgets, especially when your app is not in the foreground.

🚧
It's important to note that while Expedited Workers are fully supported on Android 12 (API level 31) and above, we must take additional steps to ensure backward compatibility on older Android versions.

On platforms older than Android 12, WorkManager may run a foreground service for expedited jobs. Foreground services typically display a notification to the user and this is exactly what you must implement.

Step 1

Create your worker class and override the methods doWork() and getForegroundInfo().

Step 1.2 - Notification Handling

In case your app doesn't already make use of notifications, you can set up one in a simple manner.

You can handle the function createNotification() such that it returns a Notification object. A sample for this is as below.

Ensure that you have handled this channel in your application class. In case you already haven't, follow the code below.

Step 2

Now, enqueue the work. What enqueue means is to schedule your task (or work) to execute.

Step 3

If you'd like to add some constraint for your work request, meaning some requirements for your work to execute, you can do so this way. This constraint handles the need for an internet connection before executing this work request.

Out Of Quota Policy

Now, you might be wondering about what RUN_AS_NON_EXPEDITED_WORK_REQUEST means in the above code. This is way to specify to the OS, that if the expedited work quota for the app runs out, run the worker as a non expedited work request.

There's another OutOfQuotaPolicy called DROP_WORK_REQUEST. As the name states, this implies that if the quota is over, just drop the work and don't bother executing it.

Based on your use case, you can choose from either of them.

Conclusion

Testing your implementation across different Android versions and devices is mandatory to ensure smooth functioning. Also, remember to respect user preferences and optimize for efficiency.

By following these steps and guidelines, you're now equipped to enhance your Android app with Expedited Workers, ensuring efficient background task management across a wide range of Android versions. Do comment below if you're stuck with anything.

In our next article, let's look at handling errors and retrying the work if something goes wrong, for instance an API failure?