ANR (application not responding)
ANR definition: On Android, if your application has been unresponsive for some time, a dialog box is displayed to the user called the application unresponsive (anr:application not responding) dialog box. The user can select wait to keep the program running, or force close. So a smooth and reasonable application can not appear in the ANR, and let the user each time to deal with this dialog box. Therefore, it is important to design the response performance in the program so that the system does not show the ANR to the user.
By default, the maximum execution time for activity in Android is 5 seconds, and the maximum execution time for Broadcastreceiver is 10 seconds.
First: What will trigger the ANR?
In Android, the responsiveness of the application is monitored by the activity Manager and the WindowManager system service. When it detects one of the following situations, Android displays the ANR for a specific application:
1. No response input event within 5 seconds (for example, press the button, touch the screen)
2.BroadcastReceiver did not complete in 10 seconds
There are many reasons for the above two points, such as doing a very time-consuming operation in the main thread, such as downloading, IO exception, etc.
Potentially time-consuming operations, such as network or database operations, or high time-consuming computations such as changing the bitmap size , should be done in the thread of the Strand (or, in the case of database operations, through asynchronous requests). However, it does not mean that your main thread is blocking there waiting for the child thread to complete-nor does it call thread.wait () or Thread.Sleep (). Instead, the main thread should provide a handler for the child thread so that it can be submitted to the main thread when it is complete. Designing your application in this way will ensure that your main thread remains responsive to the input and can avoid the ANR dialog that is raised due to the timeout of the 5-second input event.
Second: How to avoid ANR?
1, run in the main line thread any method to do as little as possible to do things. In particular, activity should create as little as possible in its critical life-cycle methods, such as OnCreate () and Onresume (). (You can do something by re-opening the sub-thread and then using Handler+message to do things like updating the UI in the main thread, etc.)
2, the application should avoid doing time-consuming operation or calculation in Broadcastreceiver. Instead of doing these tasks in a sub-thread (because of the short life cycle of broadcastreceiver), the application should start a Service if responding to intent broadcasts requires a time-consuming action. (note here that you can start the service in the broadcast recipient, but you cannot start broadcasereciver in the service, for reasons that follow, this is not the focus of this article)
3. Avoid starting an activity in intent receiver because it creates a new screen and grabs the focus from the program that the current user is running. If your application needs to show the user what to do in response to intent broadcasts, you should use Notification Manager.
Summary: The ANR exception is also in the program itself often encountered problems, the main solution to their own most commonly used is not to do time-consuming operations in the main thread, but should be placed in sub-threads to achieve, such as the use of Handler+mesage way, Or sometimes you need to do some time-consuming operation that interacts with the network to take the Asyntask asynchronous task (its underlying handler+mesage is actually a thread pool) and so on, updating the UI in the main thread.
Reasons and solutions for Android ANR generation