Android ANR exceptions and Solutions

Source: Internet
Author: User

 

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

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.