How to avoid the ANR problem in Android development?

Source: Internet
Author: User

In Android, Activity Manager and Window Manager System Services monitor the operation of each program, and the ANR prompt dialog box pops up when the program appears in the following three scenarios:

1. The user does not respond for 5 seconds after an operation has been performed.

The 2.broadCastReceiver operation was not completed within 10 seconds.

3.Service did not return the result within 20 seconds.

In the app run, it's annoying to have the ANR, which can lead to a very poor user experience. So when designing Android apps, try to avoid the ANR as much as possible. So how can we avoid the production of ANR? Just pay attention to the following three points, actually very simple:

  1. Avoid complex time-consuming operations on the main thread, such as sending and receiving network data/doing a large number of calculations/operations database/read-write files. This can be done by using Asynctask or using multithreading.

  2. Broadcastreceiver to perform complex operations, you can start a service in the OnReceive () method to handle

  3. Avoid the occurrence of synchronization/deadlock or improper error handling during the design and coding phase.

How to avoid the ANR problem in Android development?

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.