1. What is ANR
In Android, the responsiveness of the application is monitored by the activitymanager and WindowManager system services. When one of the following conditions is detected, Android displays the ANR for a specific application
A activity does not respond to input events within 5 seconds (e.g. press the button, touch the screen)
B Broadcastreceiver not executed in 10 seconds.
There are many reasons for this, the main reason is that in the main thread to do a very time-consuming operation, such as download, IO exception.
2. How to avoid ANR
Generally time-consuming operations, such as network and database access, high time-consuming calculations (changing bitmap dimensions), should be placed on the approached (or, for example, how database operations should be asynchronous requests). Instead of having the main thread block there waiting for the child thread to complete, it is not called thread.wait () or Thread.Sleep (), but the mainline thread executes provides a handler for the child thread that can be submitted to the main thread once it is completed.
Main practices:
Any method that runs on the main thread should take as little time as possible and pay special attention to the key life cycle methods such as OnCreate () and Onresume () of the activity. You can use the way to re-open a child thread, and then handler+message to handle time-consuming operations, such as updating the UI in the main thread.
B applications should avoid time-consuming operations or calculations in Broadcastreceiver. Unlike activity, the Broadcastreceiver has a short life cycle and therefore does not perform these tasks in a child thread. If the corresponding intent broadcast needs to perform a time-consuming operation, instead, let the application start a service to process.
Also avoid starting an activity in Broadcastreceiver because it creates a new interface that will focus on the user's actions and affect the user experience. If the application needs to be presented to the user when the corresponding intent is broadcast, it should be done with notification message.
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.
Http://blog.sina.com.cn/s/blog_618199e60101kvbl.html
Android ANR Causes and solutions