Causes of ANR abnormal and its solutions

Source: Internet
Author: User

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

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.