What is ANR? How to avoid it?

Source: Internet
Author: User

ANR:Application Not responding ( application unresponsive ). When the following conditions occur, Android it will show ANR dialog box:

Response to input events (such as keystrokes, touch-screen events) exceeds 5 sec Intent Receiver (intentreceiver) The second is still not finished

The Android app runs completely in a separate thread . This means that any running in the main thread,

Operations that require a significant amount of time will cause ANR. So how to avoid it? It can be used in two ways:

1.

Asynctask Asynchronous Tasks,doinbackground () and onpostexecute (Result) Two methods are very important

Doinbackground () This method runs in a background thread and is primarily responsible for performing those very time-consuming operations, such as moving

Network connection, parsing XML and other operations in the nursing system. The method must be overloaded. OnPostExecute (Result)

This method also runs on the UI thread, in the doinbackground (Params ... ) is called after the method executes, which is used to

Handles the results returned after the background task is executed.

2. Thread sub-thread + handler

  1. Handler Mhandler = new Handler () {
  2. @Override
  3. public void Handlemessage (Message msg) {
  4. super.handlemessage (msg);
  5. switch (msg.what) {
  6. Case 0:
  7. //do Something,refresh UI;
  8. Break ;
  9. Default:
  10. Break ;
  11. }
  12. }
  13. };

You can create a message object that can send objects, parameters, send messages with handler objects, receive them, judge messages, and perform UI operations.

What is ANR? How to avoid it?

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.