Anrs ("Application Not Responding"), meaning "app is not responding".
In the following cases, Android will report a ANR error:
– The main thread ("Event processing Thread"/"UI thread") does not respond to input events within 5 seconds
–broadcastreceiver did not complete the return within 10 seconds
Typically, the following practices can cause the ANR
1. Network operation within the main thread
2. Perform some slow disk operations within the main thread (for example, execute SQL queries that are not optimized)
The application should respond in 5 seconds or 10 seconds, otherwise the user will feel "this app is rubbish" "rotten" "Slow" ... Wait a minute
The logic should be
1. New Thread for data request
2. After retrieving the data, call the Handler.sendmessage method
3. Updating the UI in the handler handle () method
That's it, or it can be.
Private Thread Mthread;
Private Handler Mhandler;
OnCreate
Mthread = new Thread (runnable);//Here is a child thread created in the main thread that does not block the UI
Mthread.start ();
Private Runnable Runnable = new Runnable () {
void Run () {
Do time-consuming data processing
Processing completed
Mhandler.sendemptymessge (1000);//Here a message informs the UI that it can be updated
}
};
Mhandler = new Handler () {
void Handlemessgae (Message msg) {
if (Msg.what = = 1000) {
Update UI
}
Super.handlmessgae ();
}
};
In short, the key to solving this problem is not to do too much time-consuming operations in the main thread.
Causes of ANR abnormal and its solutions