What is ANR and how to avoid it?

Source: Internet
Author: User

Answer: ANR definition of:

in the Android , If your application has a time-sensitive response, the system prompts the user " Application Not responding " ( ANR : Application Not responding ) dialog box. Therefore, it is important to design the response performance in the program so that the system does not show the ANR to the user.

How to avoid:

Let 's start by studying why it's Android application and how best to build the application to avoid ANR.
Android applications are typically run on a separate thread (for example:main), which means that what your application does will cause a large amount of time in the main thread. ANR dialog box because your application does not give itself the opportunity to handle input events or Intent broadcasts.

Therefore, any adapting running on the mainline thread do as little as possible. In particular,Activityit should be in its critical life-cycle approach (onCreate ()and theOnresume ()as few as possible to create operations. Potentially time-consuming operations, such as network or database operations, or high time-consuming computations such as changing the bitmap size, should be done in the thread of the Strand (or, in the case of database operations, through asynchronous requests). However, not that your main thread is blocking there waiting for the completion of the child thread---nor is it calledthread.wait ()orThread.Sleep (). The workaround is that the main thread should provide a child thread with aHandler,so that it can be submitted to the main thread when it is complete. Designing your application in this way will ensure that your main thread remains responsive to input and can avoid5the time-out of the second input event is raisedANRdialog box. This practice should be shown in otherUIthreads, because they are all affected by the same timeout.

IntentreceiverA special limitation of execution time means that it should do small, trivial work in the background, such as saving settings or registering aNotification. As with other methods called in the main thread, the application should avoidBroadcastreceiverdo time-consuming operations or calculations, but are not doing these tasks in sub-threads (becauseBroadcastreceivershort life cycle), instead, if the responseIntentthe broadcast needs to perform a time-consuming action, the application should start aService. By the way, you should also avoidIntent Receiverin the start of aActivity, because it creates a new screen and grabs the focus from the program that the current user is running. If your application responds to theIntentWhat you need to show the user when broadcasting, you should useNotification Managerto achieve.

in general, in the application, - to the 200ms is the user can sense the time threshold of the block, the following summarizes some techniques to avoid ANR, and help make your application look responsive.

If your application is working in the background in response to user input, you can show the progress of your work ( ProgressBar and the ProgressDialog useful in this case). In particular the game, in the sub-thread to do mobile computing. If your program has a time-consuming initialization process, consider displaying a Splash screen or quickly displaying the main picture and populating the information asynchronously. In both cases, you should display the progress you are making in order to prevent users from thinking that the program is frozen.

What is ANR and how to avoid it?

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.