Android ANR Causes and solutions

Source: Internet
Author: User

1. What is ANR

In Android, the responsiveness of the application is monitored by the activitymanager and WindowManager system services. When one of the following conditions is detected, Android displays the ANR for a specific application

A activity does not respond to input events within 5 seconds (e.g. press the button, touch the screen)

B Broadcastreceiver not executed in 10 seconds.

There are many reasons for this, the main reason is that in the main thread to do a very time-consuming operation, such as download, IO exception.

2. How to avoid ANR

Generally time-consuming operations, such as network and database access, high time-consuming calculations (changing bitmap dimensions), should be placed on the approached (or, for example, how database operations should be asynchronous requests). Instead of having the main thread block there waiting for the child thread to complete, it is not called thread.wait () or Thread.Sleep (), but the mainline thread executes provides a handler for the child thread that can be submitted to the main thread once it is completed.

Main practices:

Any method that runs on the main thread should take as little time as possible and pay special attention to the key life cycle methods such as OnCreate () and Onresume () of the activity. You can use the way to re-open a child thread, and then handler+message to handle time-consuming operations, such as updating the UI in the main thread.

B applications should avoid time-consuming operations or calculations in Broadcastreceiver. Unlike activity, the Broadcastreceiver has a short life cycle and therefore does not perform these tasks in a child thread. If the corresponding intent broadcast needs to perform a time-consuming operation, instead, let the application start a service to process.

Also avoid starting an activity in Broadcastreceiver because it creates a new interface that will focus on the user's actions and affect the user experience. If the application needs to be presented to the user when the corresponding intent is broadcast, it should be done with notification message.

Summary: The ANR exception is also in the program itself often encountered problems, the main solution to their own most commonly used is not to do time-consuming operations in the main thread, but should be placed in sub-threads to achieve, such as the use of Handler+mesage way, Or sometimes you need to do some time-consuming operation that interacts with the network to take the Asyntask asynchronous task (its underlying handler+mesage is actually a thread pool) and so on, updating the UI in the main thread.

Http://blog.sina.com.cn/s/blog_618199e60101kvbl.html

Android ANR Causes and 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.