问题
Is the ANR an exception, an error or what? Can we actually catch it in a try{} catch(){}
structure?
回答1:
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.
回答2:
The Application Not Responding (ANR) dialog
As you can imagine, if the main thread is busy with a heavy calculation or reading data from a network socket, it cannot immediately respond to user input such as a tap or swipe.
An application that doesn't respond quickly to user interaction will feel unresponsive—anything more than a couple of hundred milliseconds delay is noticeable. This is such a pernicious problem that the Android platform protects users from applications that do too much on the main thread.
Notice :
If an app does not respond to user input within fve seconds, the user will see the Application Not Responding (ANR) dialog and will be offered the option to quit the application.
The following screenshot shows a typical Android ANR dialog:
Android works hard to synchronize the user interface redraws with the hardware-refresh rate. This means that it aims to redraw at the rate of 60 frames per second—that's just 16.67 ms per frame. If we do work on the main thread that takes anywhere near 16 ms, we risk affecting the frame rate, resulting in jank—stuttering animations, 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 into bad reviews and unpopular applications. A rule to live by when building Android applications is: do not block the main thread!
Notice :
Android provides a helpful strict mode setting in Developer Options on each device, which will flash on the screen when applications perform long-running operations on the main thread.
Further protection was added to the platform in Honeycomb (API level 11) with the introduction of a new Exception class, NetworkOnMainThreadException, a subclass of RuntimeException that is thrown if the system detects network activity initiated on the main thread.
Source :
Asynchronous Android Programming - Second Edition - Helder Vasconcelos - July 2016
来源:https://stackoverflow.com/questions/6540076/is-anr-an-exception-or-an-error-or-what