Anrs ("application not responding"), which means "application has no response".
Android reports an ANR error in the following cases:
-The main thread ("event processing thread"/"UI thread") does not respond to the input event within 5 seconds.
-Broadcastreceiver does not return results within 10 seconds.
Normally, the following operations will cause ANR
1. Perform network operations in the main thread
2. Perform some slow disk operations in the main thread (for example, executing SQL queries that have not been optimized)
The application should respond within 5 or 10 seconds. Otherwise, the user will feel "this application is very spam", "rotten", and "slow "... And so on.
The logic should be
1. Create a New thread for data requests
2. Call the handler. sendmessage method after obtaining the data
3. Update the UI in the handle () method of handler.
In this way, either.
Private thread mthread;
Private handler mhandler;
Oncreate:
Mthread = new thread (runnable); // A subthread is created in the main thread, which does not block the UI
Mthread. Start ();
Private runnable = new runnable (){
Void run (){
// Process time-consuming data
// Process completed
Mhandler. sendemptymessge (1000); // The message sending UI can be updated.
}
};
Mhandler = new handler (){
Void handlemessgae (Message MSG ){
If (msg. What = 1000 ){
// Update the UI
}
Super. handlmessgae ();
}
};