ANR:Application Not responding ( application unresponsive ). When the following conditions occur, Android it will show ANR dialog box:
Response to input events (such as keystrokes, touch-screen events) exceeds 5 sec Intent Receiver (intentreceiver) The second is still not finished
The Android app runs completely in a separate thread . This means that any running in the main thread,
Operations that require a significant amount of time will cause ANR. So how to avoid it? It can be used in two ways:
1.
Asynctask Asynchronous Tasks,doinbackground () and onpostexecute (Result) Two methods are very important
Doinbackground () This method runs in a background thread and is primarily responsible for performing those very time-consuming operations, such as moving
Network connection, parsing XML and other operations in the nursing system. The method must be overloaded. OnPostExecute (Result)
This method also runs on the UI thread, in the doinbackground (Params ... ) is called after the method executes, which is used to
Handles the results returned after the background task is executed.
2. Thread sub-thread + handler
- Handler Mhandler = new Handler () {
- @Override
- public void Handlemessage (Message msg) {
- super.handlemessage (msg);
- switch (msg.what) {
- Case 0:
- //do Something,refresh UI;
- Break ;
- Default:
- Break ;
- }
- }
- };
You can create a message object that can send objects, parameters, send messages with handler objects, receive them, judge messages, and perform UI operations.
What is ANR? How to avoid it?