About ANR
- 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 there be ANR
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.
- 1. The main thread has not processed the input event within 5 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 take a quick look at the whole process of producing this ANR.
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. Suddenly there are several important nouns, in order to understand this situation of the ANR, need to be familiar with the Android event mechanism, this time do not parse, here only 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.
2, the main thread in the execution of Broadcastreceiver onreceive function is not completed within 10 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.
3. The main thread is not executed within 20 seconds of the service's life cycle functions
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 each application process and system process function stack information is output to a/data/anr/traces.txt file , this file is the key file to analyze the cause of ANR, You'll also see CPU usage at the time in the logs, which is also important information that you can use 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
Wen/jonyliu (author of Jane's book)
Original link: http://www.jianshu.com/p/c8c15608c68e
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
Android ANR Detailed