On Android, if your application has been unresponsive for a while, the system will show the User a dialog box 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.
How to avoid considering the ANR definition above, let's look at why it happens in Android applications and how best to build applications to avoid ANR. Android applications are typically run on a separate thread (for example, main). This means that if your application does something that takes too long in the main thread, the ANR dialog will be raised because your application does not give itself the opportunity to handle input events or intent broadcasts. Soany method that runs on the mainline thread does as little as possible. In particular, activity should create as little as possible in its critical life-cycle methods, such as OnCreate () and Onresume (). 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 (). The alternative approach is toThe main thread should provide a handler for the child thread so that it can be submitted to the main thread when it is completed。 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. This practice should be emulated in other threads that display the UI because they are affected by the same timeout. The special limitation of intentreceiver execution time means it should: do small, trivial work in the background, such as saving settings or registering a notification. Like other methods that are called in the main thread, applications should avoid time-consuming operations or calculations 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. Incidentally, you should also 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. In general, 100 to 200ms is the time threshold in the application where the user can sense the block. So here are some extra tricks to avoid the ANR and help make your application look responsive. If your application is working behind the scenes in response to user input, you can show the progress of your work (ProgressBar and progressdialog are useful for this scenario). In particular the game, in the sub-thread to do mobile computing. If your application has a time-consuming initialization process, consider displaying a splash screen or quickly displaying the main picture and populating the information asynchronously. In both cases, you should display the progress you are making in order to prevent users from thinking that the application is frozen.
Android ANR produces the principle and how to avoid