Turn how to analyze the Android ANR solution

Source: Internet
Author: User

One: What is ANR

Anr:application not responding, i.e. application unresponsive

II: Type of ANR

ANR definition: On Android, if your application has been unresponsive for some time, a dialog box is displayed to the user called the application unresponsive (anr:application not responding) dialog box . The user can select wait to keep the program running, or force close. So a smooth and reasonable application can not appear in the ANR, and let the user each time to deal with this 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.

There are generally three types of ANR:

1:keydispatchtimeout (5 seconds)-- main type

The key or touch event is unresponsive for a specific time and does not respond to input events within 5 seconds (for example, press the key, touch the screen).

2 : broadcasttimeout (seconds)

Broadcastreceiver cannot be processed within a specific time (10s).

3:servicetimeout (seconds)-- small probability type

Service cannot process completion at a specific time

Three: Keydispatchtimeout

Akey or touch event is not dispatched within the specified time (key or touch events are not responding for a specific period)

The specific time-out is defined under the framework

Activitymanagerservice.java

How long we wait for until we timeout on key dispatching.

staticfinal int key_dispatching_timeout = 5*1000

Four: Why does it time out?

The time-out count is usually started by distributing the button to the app. There are two common reasons for timeouts :

(1) The current event does not have a chance to be processed (i.e. UI line is impersonating in the previous event, not completed in a timely manner or looper blocked for some reason)

(2) The current event is being processed, but not completed in time

Five: How to avoid keydispatchtimeout

1 : UI The thread tries to do only the same UI Related Work

2 : Time-consuming work (such as database operations, I/O , connecting to the network or anything else that might hinder UI thread) put it into a separate thread-handling

3 : try to use Handler to handle Uithread and other Thread the interaction between

VI: UI thread

Speaking of so many UI threads, which ones are part of the UI thread?

The UI thread mainly includes the following:

    1. Activity:oncreate (), Onresume (), OnDestroy (), OnKeyDown (), OnClick (), etc

    2. Asynctask:onpreexecute (), Onprogressupdate (), OnPostExecute (), oncancel,etc

    3. Mainthread handler:handlemessage (), post* (runnable R), etc

    4. Other

Turn how to analyze the Android ANR solution

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.