Is "ANR" an exception or an error or what? Is "ANR" an exception or an error or what? android android

Is "ANR" an exception or an error or what?


ANR (Application Not Responding) is not exactly an error. It is shown when your application is very sluggish and takes a lot of time to respond, thus making the user wait. The user won't appreciate if your application makes them wait for a long time. So, the Android framework gives the user an option of closing your application. http://developer.android.com/guide/practices/design/responsiveness.html

This occurs when you are doing long running operations on the main thread. The system can't process user interactions during this period since the main thread is blocked. The solution is to do the heavy operations in a worker thread and keep the main thread free.


The Application Not Responding (ANR) dialog

As you can imagine, if the main thread is busy with a heavy calculation or readingdata from a network socket, it cannot immediately respond to user input such as atap or swipe.

An application that doesn't respond quickly to user interaction will feelunresponsive—anything more than a couple of hundred milliseconds delay isnoticeable. This is such a pernicious problem that the Android platform protectsusers from applications that do too much on the main thread.

Notice :

If an app does not respond to user input within fve seconds, theuser will see the Application Not Responding (ANR) dialog andwill be offered the option to quit the application.

The following screenshot shows a typical Android ANR dialog:

enter image description here

Android works hard to synchronize the user interface redraws with thehardware-refresh rate. This means that it aims to redraw at the rate of 60 frames persecond—that's just 16.67 ms per frame. If we do work on the main thread that takesanywhere near 16 ms, we risk affecting the frame rate, resulting in jank—stutteringanimations, jerky scrolling, and so on.

Ideally, of course, we don't want to drop a single frame. Jank, unresponsiveness,and especially the ANR, offer a very poor user experience, which translates intobad reviews and unpopular applications. A rule to live by when building Androidapplications is: do not block the main thread!

Notice :

Android provides a helpful strict mode setting in Developer Optionson each device, which will flash on the screen when applicationsperform long-running operations on the main thread.

Further protection was added to the platform in Honeycomb (API level 11) with theintroduction of a new Exception class, NetworkOnMainThreadException, a subclassof RuntimeException that is thrown if the system detects network activity initiatedon the main thread.

Source :

Asynchronous Android Programming - Second Edition - Helder Vasconcelos - July 2016