ANR ("application not responding"), which means "the 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 input events within 5 seconds.
-Broadcastreceiver does not return results within 10 seconds.
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 to perform time-consuming operations such as network or database in a separate thread. After the operation is completed, update the UI through handler.
Private thread mthread;
Private handler mhandler;
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 ();
}
};