Android ANR exception and Solution

Source: Internet
Author: User

 

ANR Definition

On Android, if your application is not responsive for a period of time, the system will display you a dialog box called ANR: application not responding. You can choose to keep the program running, but they do not want to process this dialog box every time they use your application. Therefore, the design of response performance in the program is very important, so that the system will not display ANR to the user.

 

 

ANR troubleshooting Overview:

 

Considering the above ANR definition, let's take a look at why it occurs in Android applications and how to best build applications to avoid ANR.

 

Android applications usually run in a separate thread (for example, main. This means that if your application takes too long in the main thread, the ANR dialog box will be triggered, because your application does not have the opportunity to handle input events or intent broadcasts.

 

Therefore, any method running in the main thread should do as little as possible. In particular, the activity should do as few creation operations as possible in its key lifecycle methods (such as oncreate () and onresume. Potential time-consuming operations, such as network or database operations, or high-time computing such as bitmap size change, should be in the Child thread (or take database operations as an example, through asynchronous request). However, it does not mean that your main thread is blocked and waits for the completion of the sub-thread-or it does not call thread. Wait () or thread. Sleep (). The alternative is that the main thread should provide a handler for the sub-thread so that it can be submitted to the main thread upon completion. Designing your application in this way will ensure that your main thread is responsive to the input and avoid the ANR dialog box caused by 5 seconds of input event timeout. This approach should be followed in other display UI threads because they are all affected by the same timeout.

 

 Common Causes of ANR exceptions:

1. Perform network operations in the main thread to obtain resources on the server

2. Perform some slow disk operations in the main thread (

The application should respond within 5 or 10 seconds.

 

In general, 100 to MS is the time threshold for the user to perceive the block in the application. Therefore, here are some additional tips to avoid ANR and help make your application look responsive.

 

If your application is working in the background for the response user input, the progress of the work can be displayed (progressbar and progressdialog are useful in this case ).


Solve ANR exceptions using handler:

Method 1:

1. Start a new thread for data requests. This thread contains code that is time-consuming.
2. CallHandler. sendmessageMethod
3. Call in the main threadHandlemessgae (Message MSG)Method to receive data and update the UI

Method 2:

ExploitationHandler. Post (New runnable (){

Public void run (){

-------------------- Code for time-consuming operations

}}

);

Send a runnable to the thread that creates the handler object and automatically execute the run method.

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.