Android ANR and androidANR
There are three types of ANR:
1:KeyDispatchTimeout (5 seconds)
Buttons or touch events do not respond within a specific period of time
2: BroadcastTimeout (10 seconds)
BroadcastReceiver cannot be processed within a specific period of time
3:ServiceTimeout (20 seconds)
Service cannot be processed within a specified period of time
KeyDispatchTimeout
Akey or touch event was not dispatched within the specified time (the key or touch event does not respond within a specific time period)
The specific time-out period is defined in
ActivityManagerService. java
// How long we wait until we timeout on key dispatching.
Staticfinal int KEY_DISPATCHING_TIMEOUT = 5*1000
The timeout period generally starts when the keys are distributed to the app. There are two common causes of Timeout::
(1) The current event has no chance to be processed (that is, the UI thread is processing the previous event and is not completed in time or the logoff is blocked for some reason)
(2) The current event is being processed, but it is not completed in time
How to avoidKeyDispatchTimeout
1: The UI thread should only do work related to the UI as much as possible.
2: time-consuming work (such as database operations, I/O, network connections, or other operations that may hinder the UI thread) Put it into a separate thread for processing
3: Use Handler to handle the interaction between UIthread and other threads.