What is ANR?
Application Not Responding
The main cause of ANR:
The responsiveness of the application is monitored by the activitymanager and WindowManager system services, which can cause the ANR to be monitored when the main thread in the activity clicks more than 5 seconds to respond or if the wood responds in the broadcast UI thread for more than 10 seconds. Here's a list of the details:
- The main thread is blocked by I/O operations (the network IO is not allowed in the main thread after 4.0).
- There is a time-consuming calculation in the main thread.
The main reason for the ANR: What operations are on the main thread in Android?
- All life cycle callbacks for an activity are performed on the main thread.
- Service defaults are performed on the main thread.
- The Broadcastreceiver OnReceive callback is performed on the main thread.
- The Handlemessage,post (Runnable) of a looper that is not using a child thread is performed on the main thread of the handler.
- In addition to Doinbackground, the Asynctask callback is performed on the main thread.
How to resolve ANR:
- Use Asynctask to process time-consuming IO operations.
- Use thread or handlerthread to increase priority.
- Use handler to handle time-consuming tasks for worker threads.
- The activity's OnCreate () and Onresume () callbacks try to avoid time-consuming code.
Android Anomaly and performance optimization related interview questions-anr abnormal interview questions