What is ANR and how to avoid it

    ANR, application not response (application not responding). Android applications run completely in a separate thread (main), which means that any operation that runs on the main thread that consumes a lot of time will cause ANR.

    In general,

                       1. In each life cycle of the Activity, there is no response when the operation is performed for more than 5 seconds;

                       2. More than 10 seconds in Broadcast Receiver;

                       3. More than 20 seconds in service.

    will cause ANR.

    How to avoid it?

             Don't do time-consuming operations in the main thread. If a time-consuming operation is required, a child thread can be started to run.

    To perform a time-consuming operation, the Broadcast Receiver can start a service and hand over the time-consuming operation to the service for execution. Because its life cycle is relatively short. If you want to respond to the Intent broadcast, if you need to show something to the user, you can use the Notification Manager to achieve it.

Guess you like

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