Android Studio includes many like 16103d4b168a80 layout checker and database checker to help you investigate and understand the internal state of the application at runtime. In Android Studio Arctic Fox , we released a new inspector (Background Task Inspector) to help you monitor and debug Workers scheduled using WorkManager 2.5.0 or higher in your application.
For asynchronous tasks running in the background, even after the application is closed, WorkManager is recommended. Although the task can be easily configured as the Worker of the WorkManager, it is difficult to monitor its execution after the Worker is added to the queue, and it is not convenient to debug when it encounters problems.
You can easily monitor the working status of a Worker through the background task inspector, view 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 sample project to see what the background task inspector can do.
I will use architectural-components warehouse to demonstrate the background task inspector (you need to set the versions.work in the versions.gradle in the project to 2.5.0 or higher to make the Background Task Inspect better jobs). If you want to give it a try, you can check out the warehouse and try it along with reading the article. The application uses WorkManager to apply the filters selected by the user to the selected photos. Users can select a picture in the gallery through the application or simply use a stock photo. To demonstrate how the background task inspector works, I will 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 Worker. After a while, the app will display the image with the selected filter applied. Without knowing the sample application, let’s see what else I can know through the background task inspector.
Select 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 column in the App Inspection window, I ran the app again on a device/emulator with API level 26 or higher. If the application is not automatically selected, select the application process in the drop-down menu. After connecting to the application process, you can go back to the application I am running, select all the filters and click "APPLY". At this point, I can see the list of running jobs in the background task inspector.
△ List of running jobs
The background task checker lists the class names, current status, start time, number of retries, and output data of 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, WorkContinuation and Results of Worker. Let's take a closer look at each part.
△ Work Details
The Description section lists the Worker's fully qualified package name, the label assigned to it, and its UUID.
△ Execution
Next, the Execution section shows the constraints of the Worker (if any), the operating frequency, the status, and which class created the worker and added it to the queue.
△ WorkContinuation
The WorkContinuation section shows the position of the Worker in the work chain. You can check the previous, next, or other workers in the work chain (if any). You can navigate to its details by clicking on the UUID of another Worker. In this work chain, I can see that the application uses 5 different Workers. The number of workers may vary depending on the filter selected by the user.
This is a great feature, but when you are faced with an unfamiliar application, you may not be able to imagine the work chain. Another great feature of the background task inspector is that it can display the work chain in a graphical form. Just click the "Show in graph" link in the WorkContinuation section or click the "show Graph View" button at the top of the job list to switch to the Graph View.
△ Graph View
Graph View can help you understand the sequence of workers, the data passed between different stages, and their respective states.
△ Results
The last section of the Work Details panel is the Results pane. In this section you can see the start time, number of retries and output data of the selected Worker.
Now suppose I want to test what happens when a Worker stops. In order to achieve this, I will run the application again, select Worker, wait for its status to become running, and click the "Cancel Selected Work" button in the upper left corner. Once I do this, the status of the Worker I selected and the remaining Workers in the chain will become Canceled.
△ You can cancel any running Worker
If your application contains complex chained relationships like this, Graph View will be useful. In this diagram, you can quickly view the relationship between a complex group of Workers and monitor their progress.
△ WorkManager Art Show =)
If you want to use the background task inspector to try some more complicated graphics or make some WorkManager art, please refer to the DummyWorker code and add its to the continuation object .
The background task checker will be launched with the release of Android Studio Arctic Fox, but you can now latest Arctic Fox version ! If your application uses WorkManager, please try it and tell us what you think, or share your WorkManager art with us!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。