ANR in Android

Source: Internet
Author: User

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 ();
}
};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.