In the last article, we mentioned that the modern WorkManager API has also made improvements to tool support. In this article, we’ll look at some of the improvements in a real-world example. If you prefer to see this in video, check it out here.

Starting with Android Studio Arctic Fox, you can check and debug workers directly within Android Studio using the background task inspector.

The background task inspector is part of the application inspection suite and provides richer and more detailed information on several aspects of the application. To get the most out of the inspector, your application needs to use WorkManager 2.5 or later.

Apply the inspector enable method

The quickest way to open the App Inspector for the first time is to hold down the “Shift” key twice, then type App Inspection in the pop-up dialog and press Enter to confirm. You can also open the Application Inspector panel by selecting View → Tool Window → App Inspection from the menu bar.

△ Open the App Inspection panel

Example demonstrates

This demo uses an Example of WorkManager available on Android Studio Bumblebee and GitHub.

Switch to the Background Task Inspector TAB in the App Inspection panel, check Filter in the App, and then click the Apply button. It can be seen that with the continuous filtering, the state of each Worker in its life cycle is constantly changing. Since these workers run in order, the CleanupWorker needs to be completed before the next Worker starts. All other workers are either in the queuing state or in the blocking state.

Use the WorkManagerSample application and the application inspector panel

Now that the CleanupWorker is complete, the other workers in the queue will run in order.

Use the chart view of the Application Inspector panel

You can also view the contents of the entire continuation queue from an image similar to the diagram. Select one of the workers and switch to the chart view (the buttons are flowchart-style). For more complex queues, the chart view is an efficient and indispensable means of understanding. From either view, you can directly click on any Worker for a more detailed analysis.

△ Details of background tasks

At the top is basic descriptive information, including a clickable class name and a unique ID.

Database inspector

△ Database inspector panel

Earlier we talked about how WorkManager will persist your working data, so let’s take a closer look at how this is done using the Database Inspector. You can click on the Database Inspector TAB and look for a unique ID for a job. You will find that all information about that particular execution is saved.

Background task checker

You can also take a closer look at which queue a particular execution is placed in. Compared with manually searching for call information, this organized approach can significantly improve the efficiency of analysis. You can see if a particular Worker is restricted, as well as more detailed frequency and execution status information.

The WorkManager can use the output data of one Worker and pass it to another Worker downstream from the pipeline. You can see this in the Results of the task details. The output data contains the unique identifier of each Worker and will be transmitted in queue order.

△ Demonstrate the scenario of Worker execution failure

Here shows you the scenario when a Worker in the work chain fails to execute. An error Worker was inserted into the continuation queue of work, and the application was restarted. As you can see, the results of the last run are still here for you to examine. This further reinforces the working data persistence feature mentioned earlier. Start and apply the filter here, select Worker and click the chart view again, so you can quickly locate where the work is starting to go wrong (note the exclamation mark here). You can click from here to go directly to the Worker view and continue debugging the broken session.

To learn more

This article has given you a brief overview of the new background task checker. That’s all about WorkManager. For more resources, see:

  • The modern WorkManager API has been released
  • WorkManager series article: Customize WorkManager with Dagger
  • WorkManager documentation: Schedule tasks using WorkManager
  • Codelab: Use WorkManager to handle background tasks
  • Codelab: Advanced knowledge of WorkManager
  • WorkManager sample code

Please click here to submit your feedback to us, or share your favorite content or questions. Your feedback is very important to us, thank you for your support!