One: What is ANR
Anr:application not responding, i.e. application unresponsive
II: Type of ANR
There are generally three types of ANR:
1:keydispatchtimeout (5 seconds)-- main type
Key or touch events do not respond at a specific time
2:broadcasttimeout (seconds)
Broadcastreceiver cannot process completion at a specific time
3:servicetimeout (seconds)-- small probability type
Service cannot process completion at a specific time
Three: Keydispatchtimeout
Akey or touch event is not dispatched within the specified time (key or touch events are not responding for a specific period)
The specific time-out is defined under the framework
Activitymanagerservice.java
How long we wait for until we timeout on key dispatching.
staticfinal int key_dispatching_timeout = 5*1000
Four: Why does it time out?
The time-out count is usually started by distributing the button to the app. There are two common reasons for timeouts :
(1) The current event does not have a chance to be processed (i.e. UI line is impersonating in the previous event, not completed in a timely manner or looper blocked for some reason)
(2) The current event is being processed, but not completed in time
Five: How to avoid keydispatchtimeout
1:UI thread tries to do only UI -related work
2: Time-consuming work (such as database operations,I/O, network connections, or other actions that might hinder the UI thread) putting it into a separate thread processing
3: Try to use Handler to handle the interaction between Uithread and other thread
VI: UI thread
Speaking of so many UI threads, which ones are part of the UI thread?
The UI thread mainly includes the following:
-
Activity:oncreate (), Onresume (), OnDestroy (), OnKeyDown (), OnClick (), etc
-
Asynctask:onpreexecute (), Onprogressupdate (), OnPostExecute (), oncancel,etc
-
Mainthread handler:handlemessage (), post* (runnable R), etc
-
Other
(RPM) How to analyze and resolve Android ANR