6

When an Android application is not responding, an ANR dialog would pop up. My question is: how does the Android platform detect ANR and make the decision to initiate such a pop-up dialog.

dacongy
  • 2,504
  • 3
  • 30
  • 48
  • From http://developer.android.com/training/articles/perf-anr.html, under the sub-heading **"What Triggers ANR?"**: *"Generally, the system displays an ANR if an application cannot respond to user input. For example, if an application blocks on some I/O operation (frequently a network access) on the UI thread so the system can't process incoming user input events.*" – Robert Harvey Jan 30 '13 at 20:45

1 Answers1

4

Its based on the UI thread and how much time it takes the UI thread to return to the message loop (the message loop is hidden from you as the app developer, but there's a giant loop on your UI thread that receives messages for user input, draw requests, AsyncTask completion, etc). Basically you have a fixed amount of time per message before it declares an ANR. Time spent sleeping waiting for a message will never cause an ANR. There's a separate watchdog process in Android that checks how long its been since the thread last got to the top of that loop, and if its too long it kills it and show the ANR dialog.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127