ANR Abnormal Summary

In Android, ANR exception is an exception that often occurs for beginners. This exception is also a unique feature of Android. So how to solve ANR exception, then you must first understand what ANR exception is, how ANR exception is generated, and then to resolve ANR exceptions.

1. What is an ANR anomaly?

An abnormal ANR means that the application does not respond. The Android system needs to complete some events within a certain time range. If it fails to get an effective response after the predetermined time or the response time is too long, ANR will be caused.


2. So which scenarios will cause ANR?

        1) Service Timeout: The service is not completed within 20s;
2) BroadcastQueue Timeout: For example, the foreground broadcast is completed within 10s;
3) ContentProvider Timeout: The content provider times out;

4) inputDispatching Timeout: The input event dispatch timeout is 5s, including the key dispatch event timeout.


3. Common situations that lead to ANR:

        1) I/O blocking;
2) Network blocking;
3) onReceiver execution time exceeds 10s;

4) Multi-thread deadlock;


4. How to avoid ANR exceptions:

        1) The UI thread should only do UI-related work as much as possible;
2) Time-consuming work (such as database operations, I/O, network operations) is handled by a separate worker thread;

3) Use Handler to handle the interaction between UIthread and work thread;

       

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325509621&siteId=291194637