[Forwarding]android System Stability-ANR (iii)

Source: Internet
Author: User

Articles are original, reproduced please indicate the source, without permission and the misappropriation of legal liability.
It was written a long time ago, and it's a little wasted and shared.
Written by: Li Wendong

http://rayleeya.iteye.com/blog/1956056


1.4 How to avoid ANR
1.4.1 ANR occurs in the main thread, do not block the main thread
All standard components of an Android application run in a single main thread, and any time-consuming operations in the main thread are likely to cause ANR, because these time-consuming operations will cause the main thread to have no opportunity to handle user input events or broadcast events.
Therefore, any function that executes in the main thread should do as little as possible, especially for the activity's life cycle function. Network and database operations, as well as time-consuming operations such as bitmap transformations, should be done in sub-threads. The main thread does not need to wait for the child thread to execute, and the main thread should create a handler object that it binds to, and notifies the main thread through handler when the child thread finishes executing.
There are many ways to create child threads, and there are a number of related APIs available in Android, such as Handlerthread, Asynctask, Asyncqueryhandler, and of course, a simple thread or thread pool can be created. It is important to note, however, that you do not create too many "wild" threads, such as in Android native code, where you often see a new thread object in a function that is not managed after the start function is invoked. I do not agree with this type of writing, the advantage of this writing is to avoid the child thread to some object strong reference, so as not to avoid memory leaks, but there is a potential risk will cause "thread leak", that is, you may do the same operation multiple times to create a large number of child threads, While these sub-threads are likely to be blocked for some reason and can not exit gracefully, a large number of threads themselves will consume memory and CPU, seize critical resources, and perform more repetitive operations. So I propose to "manage" the child thread, whether it is using the flag or the member variable, in short, do not let the thread arbitrarily be created, with a limited number of threads or thread pool processing all requests, can use handler to queue the request, eliminate the duplication of requests to reduce the waste of resources, You should also consider stopping the child threads at the appropriate time, such as when the activity is destroyed, to avoid unnecessary operations and memory leaks.
Broadcastreceiver is often used to perform small, trivial tasks in the background, such as saving program settings. Do not perform long-running operations in the BR, which should be placed in serivce. Also, do not start an activity in the BR, because that creates a new window, the new window grabs the screen focus, and the user is no longer receiving input events when the window loses focus. The sudden appearance of a new window may surprise the user, or it may appear that the old window's Layer (window's Type property) is high and the new window is not visible, but the input event is robbed by the new window. If you have a message in the BR to prompt the user, consider using Notificationmanager to display the notification in the status bar to achieve the purpose.
1.4.2 Eliminate the sense of pause
Usually people feel that the interface has a sense of pause for 100 to 200 milliseconds, so in order to give users a better experience in the avoidance of ANR on the basis of further to eliminate the sense of pause, there are several suggestions:
If the application is performing time-consuming work in the background, you can use ProgressBar or ProgressDialog to prompt the user for work progress;
For the application of the game class, the sub-thread should be used to do the location calculation;
If your application's initialization process is time-consuming, you can display a scene animation or picture at initialization, or you can quickly display the main interface and then asynchronously load the initialization data.

[Forwarding]android System Stability-ANR (iii)

Related Article

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.