SoFunction
Updated on 2025-03-11

Analysis of Android ANR (Application Not Responding)

Analysis of Android ANR (Application Not Responding)

ANR (Application Not Responding)

ANR definition: On Android, if your application is not responsive enough for a period of time, the system will display a dialog box to the user. This dialog box is called the Application Not Responding (ANR: Application Not Responding) dialog box. Users can choose "Wait" to keep the program running, or they can choose "Forced Close". Therefore, anr cannot appear in a smooth and reasonable application, but let users deal with this dialog box every time. Therefore, the design of response performance is very important in the program, so that the system will not display the ANR to the user.

    By default, the maximum execution time of Activity in Android is 5 seconds, and the maximum execution time of BroadcastReceiver is 10 seconds.

First: What triggers ANR?

In Android, the responsiveness of the application is monitored by Activity Manager and WindowManager system services. Android displays ANR for a specific application when it monitors one of the following situations:

1. No response to input events within 5 seconds (for example, key press, screen touch)

No execution was completed within 10 seconds

There are many reasons for the above two points, such as doing very time-consuming operations in the main thread, such as downloading, io exceptions, etc.

Potential time-consuming operations, such as network or database operations, or high-time-consuming calculations such as changing the bitmap size, should be done in the child thread (or taking database operations as an example, through asynchronous request). However, it's not that your main thread is blocking there waiting for the child thread to complete—it's not that it's called () or (). Alternatively, the main thread should provide a handler for the child thread so that it can be submitted to the main thread when completed. Designing your application in this way will ensure that your main thread remains responsive to inputs and avoid ANR dialogs caused by timeouts of 5 second input events.

Second: How to avoid ANR? 

1. Do as little as possible any method running in the main thread. In particular, Activity should do as few creation operations as possible in its critical lifecycle methods (such as onCreate() and onResume()). (You can reopen the child thread and then use Handler+Message to do some operations, such as updating the ui in the main thread, etc.)

2. Applications should avoid time-consuming operations or calculations in BroadcastReceiver. But no longer do these tasks in the child thread (because the BroadcastReceiver has a short life cycle), instead, if a time-consuming action is required to be performed in response to an Intent broadcast, the application should start a Service. (It should be noted here that you can start Service in the broadcast recipient, but you cannot start Broadcasereciver in the Service. There will be an introduction to the reasons in the future, and this is not the focus of this article)

3. Avoid starting an activity in the Intent Receiver, because it will create a new screen and grab focus from the program that the current user is running. If your application needs to show what it needs to the user when responding to Intent broadcasts, you should use Notification Manager to implement it.

Summarize:The anr exception is also a problem that I often encounter in the program. The main solution is that I should not do time-consuming operations in the main thread, but put them in the child thread. For example, if I use the Handler+mesage method, or sometimes I need to do some time-consuming operations that interact with the network, I use the asyntask asynchronous task method (the difference between Handler+mesage is actually a thread pool) and so on, and update the UI in the main thread.

The above is a detailed explanation and solution to Android ANR. If you have any questions, please leave a message or communicate and discuss in this website community. Thank you for reading. I hope it can help you. Thank you for your support for this website!