How does the Android platform detect ANR problems?

When the Android application does not respond, the ANR dialog box opens. My question is: how does the Android platform detect ANR and decide to initiate such a popup dialog.

+4
source share
1 answer

It is based on the user interface thread and how long it takes for the user interface thread to return to the message loop (the message loop is hidden from you as the application developer, but there is a giant loop in the user interface thread that accepts messages for user input, drawing requests, completion AsyncTask etc.). Basically, you have a fixed amount of time per message before it announces ANR. Time spent sleeping waiting for a message will never call ANR. Android has a separate watchdog process that checks how much time has passed since the last thread hit the top of this loop, and if it kills it for too long and displays the ANR dialog.

+1
source

All Articles