Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Android platform detect ANR problems?

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.

like image 889
dacongy Avatar asked Jan 30 '13 20:01

dacongy


People also ask

How do you detect ANR?

There are some common patterns to look for when diagnosing ANRs: The app is doing slow operations involving I/O on the main thread. The app is doing a long calculation on the main thread. The main thread is doing a synchronous binder call to another process, and that other process is taking a long time to return.

What is ANR in Android What are the measures you can take to avoid ANR?

ANR stands for Application Not Responding. An ANR will occur if you're running a process on the UI thread which takes an extended time, usually around 5 seconds. During this point, the GUI (Graphical User Interface) will lock up which can end in anything the user presses won't be actioned.

Which of the following actions are likely to cause ANR errors?

Two conditions may cause an ANR error on an Android device: An active app does not respond to an input event within 5 seconds. The BroadcastReceiver class does not finish executing after a long period of time.

What is the difference between ANR and crash in Android?

ANR is more like a time-out than a crash, because sometimes the non-responsive app is stuck open, instead of fully crashing and closing.


1 Answers

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.

like image 156
Gabe Sechan Avatar answered Oct 07 '22 22:10

Gabe Sechan