Android Studio includes a number of inspectors, such as layout checker and database checker, to help you investigate and understand the internal state of your application at run time. In Android Studio Arctic Fox, we’ve released a new Background Task Inspector, Used to help you monitor and debug workers that are scheduled in your application using WorkManager 2.5.0 or later.

WorkManager is recommended for asynchronous tasks running in the background, even after the application has been shut down. Although the task can be conveniently configured as the Worker of WorkManager, it is difficult to monitor the execution of the Worker after it is added to the queue, and it is not convenient to debug when problems occur.

Background Task checker allows you to easily monitor the working status of a Worker, see its relationship with other workers linked to it, or check the output, frequency, and other time-related information of the Worker. Let’s take a look at what the background task inspector can do through a sample project.

I’ll use the WorkManager example application from the Architectural Components repository to demonstrate the background task inspector (set versions. Work in versions. Gradle to 2.5.0 in the project) Or later to make Background Task Inspect work better). If you want to give it a try, check out the repository and try it out as you read the article. The app uses WorkManager to apply the filter selected by the user to the selected photos. Users can use the app to select an image from the gallery or simply use a stock photo. To demonstrate how the Background Task Inspector works, I’ll run the application and select an image to apply the filter.

Apply the selected filter to the selected image

These filters are implemented as WorkManager workers. Wait a moment and the app will show you the image with the selected filter applied. Without knowing the sample application, let’s see what else I can learn from the Background Task inspector.

Choose View > Tool Windows > App Inspection on the menu bar to open the background task Inspector.

△ View > Tool Windows > App Inspection

After selecting the Background Task Inspector bar in the App Inspection window, I run the application again on devices/emulators with API level 26 or higher. If no application is automatically selected, select an application process from the drop-down list. Once I’m connected to the app process, I can go back to the app I’m running, select all the filters and click “APPLY”. At this point I can see a list of running jobs in the Background Task inspector.

△ List of running jobs

The Background Task inspector lists the class names, current status, start time, retry times, and output data for all running, failed, and completed jobs. Click on a job in the list to open the Work Details panel.

△ Work Details panel

This panel provides the Description, Execution, continuation, and Results of the Worker. Let’s take a closer look at each part.

Delta Work Details

The Description section lists the Worker containing the fully qualified package name, the label assigned to it, and its UUID.

Delta Execution

Next, the Execution section shows the Worker’s constraints (if any), run frequency, state, and which class created and queued the Worker.

Delta WorkContinuation

The WorkContinuation section shows where the Worker is on the work chain. You can check the previous Worker, the last Worker, or the other Worker (if any) on the work chain. You can navigate to the details of another Worker by clicking on its UUID. On this work chain, I can see that the app is using five different workers. The number of workers may vary depending on the filter selected by the user.

This is a nice feature, but you can’t necessarily imagine working chains when you’re dealing with unfamiliar applications. Another nice feature of the Background Task Inspector is its ability to graphically display the chain of work. Simply click the “Show in Graph “link in the Continuation section or click the “Show Graph View” button at the top of the job list to switch to the Graph View.

Delta Graph View

The Graph View helps you understand the order of the Worker, the data that passes between the stages, and their individual states.

Delta Results

The last section of the Work Details panel is the Results pane. In this section you can see the start time, retry times, and output data of the selected Worker.

Now suppose I want to test what happens when a Worker stops. To achieve this, I will run the app again, select the Worker, and click the “Cancel Selected Work” button in the upper left corner after its state changes to running. Once I do this, the status of my selected Worker and the remaining workers in the chain will change to Canceled.

You can cancel any running Worker

Graph View can be useful if your application includes complex chained relationships like this. You can quickly view the relationships between a complex set of workers and monitor their progress in this diagram.

▽ WorkManager Art Exhibition =)

If you want to try some more complex graphics with the background Task inspector or make some WorkManager art, see the DummyWorker code and add it to the Continuation object.

Background Task Checker will be available with the release of Android Studio Arctic Fox, but you can try it out in the latest Arctic Fox release right now! If your application uses WorkManager, give it a try and tell us what you think, or share your WorkManager art with us!