This series of articles is dedicated to filling in gaps and preparing Android developers for interviews.

All articles are first published on the public account “JaqenAndroid” and will be updated in the long term.

Due to the limited level of the author, the summary of the content will inevitably be wrong, welcome to point out that we study together, exchange, progress.

What is the ANR

ANR(Application Not responding) is an Application that is unresponsive; simply put, the user interface suddenly freezes and fails to respond to user actions (such as touch events).

The Android system needs to complete some events within a certain time range. ANR will be caused if the response time is too long or the response time is not effective after the scheduled time. Generally, after ANR, a dialog box is displayed indicating that the current application does not respond. You can continue to wait or close the application.

A scenario

  • InputDispatching Timeout: Failed to respond to the screen touch event or keyboard input event within 5 seconds.

  • BroadcastQueue Timeout: It is used for 10s when the BroadcastReceiver onReceive() function is executed, and 60s in the background.

  • Service Timeout: Indicates that the foreground Service is not executed within 20 seconds.

  • ContentProvider Timeout: The ContentProvider publish is not complete within 10 seconds.

How to avoid

The basic idea is to process IO operations on the worker thread, reducing other time-consuming operations and errors. Such as network requests, Socket traffic, SQL operations, file reads and writes, or operations that might block the UI thread are placed in the child thread.

ANR analysis

When ANR occurs, Logcat prints a log similar to the following:

/com.wuzy.anrtest I/zygote64: Thread[3,tid=6428,WaitingInMainSignalCatcherLoop,Thread*=0x7b3965ca00,peer=0x171c0020,"Signal Catcher"]: reacting to signal 3/com.wuzy.anrtest I/zygote64: Wrote stack traces to '/data/anr/traces.txt'/com.wuzy.anrtest I/Choreographer: Skipped 6000 frames!  The application may be doing too much work on its main thread.Copy the code

After each ANR is generated, the system writes new log data to /data/ ANR/testamp.txt.

Command to get logs:

adb shellcat /data/anr/traces.txt > /mnt/sdcard/traces.txtexitadb pull /sdcard/traces.txtCopy the code

Here I simulate an ANR situation by calling Thread.sleep in the button click event and checking the contents of the file to see the Thread name, Thread priority, Thread ID, Thread state, and the reason for the ANR.

----- pid 13872 at 2019-11-14 15:18:50 -----Cmd line: com.wuzy.anrtest."main" prio=5 tid=1 Sleeping  | group="main" sCount=1 dsCount=0 flags=1 obj=0x735f1ad0 self=0x7b396a3a00  | sysTid=13872 nice=-10 cgrp=default sched=0/0 handle=0x7b3e6f69b0  | state=S schedstat=( 832049486 21080201 432 ) utm=77 stm=6 core=5 HZ=100  | stack=0x7fe0065000-0x7fe0067000 stackSize=8MB  | held mutexes=  at java.lang.Thread.sleep(Native method)  - sleeping on <0x026e2fdc> (a java.lang.Object)  at java.lang.Thread.sleep(Thread.java:386)  - locked <0x026e2fdc> (a java.lang.Object)  at java.lang.Thread.sleep(Thread.java:327)  at com.wuzy.anrtest.MainActivity$1.onClick(MainActivity.java:19)  at android.view.View.performClick(View.java:6291)  at android.view.View$PerformClick.run(View.java:24931)  at android.os.Handler.handleCallback(Handler.java:808)  at android.os.Handler.dispatchMessage(Handler.java:101)  at android.os.Looper.loop(Looper.java:166)  at android.app.ActivityThread.main(ActivityThread.java:7523)  at java.lang.reflect.Method.invoke(Native method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)Copy the code

ANR monitoring mechanism

Android applications are message-driven. In a sense, Android is a message-driven system, with UI, events, and life cycles all linked to message processing mechanisms. Android’s ANR monitoring mechanism is the same, mostly using Android’s messaging mechanism.

In Android, the system service actually controls the life cycle callbacks of each component, so you can start the timer at this logical entry and use the Handler mechanism to process the delayed message. If there is a timeout, the ANR event message is processed. If there is no timeout, the delayed message in the queue is cancelled. You don’t have an ANR.

Specific source details, recommended reading:

Thoroughly understand android app non-responsiveness [1]

Understand the trigger principle of Android ANR [2]

Understand the information collection process of Android ANR [3]

The resources

No response mechanism thoroughly understand the android application: http://gityuan.com/2019/04/06/android-anr/

Understand Android ANR trigger principle: http://gityuan.com/2016/07/02/android-anr/

Understand the Android ANR information gathering process: http://gityuan.com/2016/12/02/app-not-response/

scan