ANR: Appliaction Not Responding. On an Android device, if the main thread of the application process responds to a timeout, ANR is generated. The system displays a prompt box to the user. “Application is not responding” has two buttons, one to wait and one to force closure.

Common types of ANR are:

KeyDispatchimeout(5 seconds) 2.BroadcastTimeout(10 seconds) The BroadcastReceiver cannot complete the process within a specified period of time. 3. A Service with a low probability cannot be processed within a certain period of time

When ANR is triggered:

  • The response of an application in Android is monitored by the Activity Manager and WindowManager system services, and the ANR is displayed for a specific application when one of the following conditions is detected

The main thread of the application process does not respond to events within 5 seconds (for example, the BroadcastReceiver does not respond when the BroadcastReceiver is pressed). The BroadcastReceiver does not complete execution within 10 seconds. The main thread of the application process does not complete the execution of the life cycle functions of the Service for 20 seconds

The general reason for the above situation is that there are time-consuming operations in the main thread, or the thread blocks, hangs, and loops. Of course, there are other external factors, such as high CPU usage, or frequent file reads and writes, etc., which can lead to ANR.Copy the code

How to avoid ANR:

If the BroadcastReceiver needs to perform time-consuming operations (such as network requests and database operations), it needs to start the sub-threads to process them. If the BroadcastReceiver has time-consuming operations, they can be processed in the Service