Articles are original, reproduced please indicate the source, without permission and the misappropriation of legal liability.
It was written a long time ago, and it's a little wasted and shared.
Written by: Li Wendong
http://rayleeya.iteye.com/blog/1955652
If you're an Android app developer, there are three things that are unavoidable in your life: death, taxes, and ANR. This is exaggerated, but due to the design of Android itself, as well as the shortcomings of applications and systems in the development process, there are often various ANR problems encountered during testing. Less in functional testing, mainly in stress tests (e.g. monkey testing), there are a lot of ANR problems. The purpose of this chapter is to summarize the various ANR problems encountered in the work of the author, summed up a set of methods to analyze and deal with the ANR problem, hope to be able to provide ideas through this set of methods, effectively reduce the time you deal with the ANR problem. It also gives some best practices to avoid ANR, more from prevention, and less remedial work.
Considering that most of the readers of this article are developers with practical experience, I will try to make less mention of some basic concepts, and I also hope to give you more "dry".
The main contents of this chapter are as follows:
ANR Introduction (What is the ANR, why there is the ANR, the abnormal length of the ANR)
How to analyze ANR (causes the ANR to classify, analyze the tool of ANR)
Example explanation
Best practices for avoiding ANR (learn from mistakes)
1.1 ANR Introduction
ANR, is the abbreviation for "Application Not Responding", which is "application unresponsive". In Android, Activitymanagerservice (AMS) and Windowmanagerservice (WMS) Monitor the response time of the application, If the application main thread (that is, the UI thread) has not finished processing the input event during the timeout period, or if a particular operation has not been performed, the ANR will appear. The anr,android generated when the input event is not processed will display a dialog box prompting the user that the current application is not responding, and the user can choose to continue waiting or closing the application (that is, the process of killing the application).
Why would 1.1.1 have ANR?
As mentioned above, the production of ANR needs to meet three conditions simultaneously:
Main thread: Only the application process's main thread response time-out will produce ANR;
Time-out: the context of the ANR is different, the time-out will be different, but as long as there is no response within the time limit will be ANR;
Input Event/Specific action: Input event refers to the key, touch screen and other device input events, the specific operation refers to the broadcastreceiver and service life cycle of the various functions, resulting in the context of the ANR different, resulting in the cause of the ANR will be different;
For these three conditions, the following three scenarios will trigger the ANR, which is described in detail below.
The main thread pair input event 5 no processing completed in seconds
Android's event system from 2.3 to do a completely different implementation, the original 2.2 is implemented in the Java layer, but in 2.3 to the overall transfer to the C + + layer, this book is based on the version of 2.3 is described. Let's start with a quick look at the whole process of producing this ANR <!--before this chapter, I want to talk about the Android event mechanism first. Or put it in the next chapter? Inclined to put in the back. --.
When the application's window is in the active state and is able to receive input events (such as keystroke events, touch events, and so on), the system's underlying escalated events are distributed to the application by Inputdispatcher. The main thread of the application reads the input events through Inputchannel and gives the interface view processing, the interface view is a tree structure, the decorview is the root of the view tree, and the event is passed from the root layer to the endpoint (for example, a button). We typically register a listener to receive and process events, or create custom view controls to handle events.
Inputdispatcher runs in a separate thread in the system process (the process named System_server), and the main thread of the application is in the process of handling the event, and Inputdispatcher constantly detects if the process is timed out, The WMS's NOTIFYANR function is notified through a series of callbacks, and eventually the case is called to show_not_responding_msg in the Mhandler object in AMS, and the System prompt dialog box is displayed. You can also see the information about ANR by using the Logcat command to view log information. Inputdispatcher is the guy who likes to "snitch".
Suddenly there are several important nouns, to understand this situation of the ANR, need to be familiar with the Android event mechanism, the book will be detailed analysis in other chapters, here just need to remember their features:
window: specifically refers to the Phonewindow object, which represents a window that can be displayed, which can receive various input events distributed by the system;
Inputdispatcher: Distributes the system escalated input events to the currently active window;
Inputchannel:inputdispatcher and applications run in two different processes, and Inputdispatcher passes the event object to the application process through Inputchannel.
Note: This ANR is generated on the premise that there is an input event, and if the user does not trigger any input events, even if the main thread is blocked, the ANR is not generated because Inputdispatcher does not distribute events to the application. Of course also does not detect processing timeouts and reporting ANR.
The main thread is executing Broadcastreceiver of the OnReceive function when Ten no execution completed in seconds
The OnReceive function of Broadcastreceiver (BR) runs in the main thread, which triggers ANR when the function does not return for more than 10 seconds. However, the ANR system for this situation does not display a dialog box prompt, only the output log.
The main thread is executing Service the various life cycle functions - no execution completed in seconds
The various life cycle functions of the service also run in the main thread, which triggers ANR when these functions do not return for more than 20 seconds. Also, the ANR system for this situation does not display a dialog box prompt, only the output log.
Only the 1th of three kinds of ANR will display the System prompt dialog box, because the user is doing interface interaction, if there is no response for a long time, will let the user suspect that the device freezes, most people will start to disorderly press, or even unplug the battery restart, to the user's experience is certainly very bad.
Three kinds of ANR will output error message in log, you will find that the function stack information of each application process and system process is output to a/data/anr/ In Traces.txt's file, this file is a key file for analyzing the causes of ANR, as well as the CPU usage at that time in the log, which is also important information, which in later chapters will detail how to use them to analyze the ANR problem.
These three types of ANR are not isolated and may affect each other. For example, in an application process, there is both an activity being displayed and a broadcastreceiver that is processing the message, both of which run in the main thread of the process. If the OnReceive function of BR is not returned, when the user taps the screen, and the onreceive is still not returned for more than 5 seconds, the main thread cannot handle the user input event, which causes the 1th type of ANR. If the continuation is not returned for more than 10 seconds, it will cause the 2nd type of ANR.
[Forwarding]android System stability-ANR (i)