一:什么是ANR
ANR:Application Not Responding,即应用无响应,如下图
二:ANR的类型
ANR一般有三种类型:
1. KeyDispatchTimeout(5 seconds) --主要类型按键或触摸事件在特定时间内无响应
2. BroadcastTimeout(10 seconds) --BroadcastReceiver在特定时间内无法处理完成
3. ServiceTimeout(20 seconds) --小概率类型 Service在特定的时间内无法处理完成
三:ANR LOG分析如下所示,
导致ANR在月com.ktc.launcher/.LauncherActivity中,原因是: Input dispatching timed out
07-24 15:55:36.853 1932 1973 E ActivityManager: ANR in com.ktc.launcher (com.ktc.launcher/.LauncherActivity)
07-24 15:55:36.853 1932 1973 E ActivityManager: Reason: Input dispatching timed out (Waiting to send key event because the focused window has not finished processing all of the input events that were previously delivered to it. Outbound queue length: 0. Wait queue length: 1.)
07-24 15:55:34.650 1932 2177 D TvCecManager: getCECListCnt, paras hdmi_port = 127
07-24 15:55:34.651 2298 3063 I CECManager_JNI: getCECListCnt
07-24 15:55:34.651 1932 2177 D TvCecManager: return hdmi_port 0
07-24 15:55:34.794 1932 1973 I Process : Sending signal. PID: 2835 SIG: 3
07-24 15:55:34.795 2835 2846 I art : Thread[2,tid=2846,WaitingInMainSignalCatcherLoop,Thread*=0xb75a9e30,peer=0x12c440a0,"Signal Catcher"]: reacting to signal 3
07-24 15:55:34.821 1932 1973 I Process : Sending signal. PID: 2285 SIG: 3
07-24 15:55:34.821 2848 2854 I art : Wrote stack traces to '/data/anr/traces.txt'
07-24 15:55:34.821 2285 2297 I art : Thread[2,tid=2297,WaitingInMainSignalCatcherLoop,Thread*=0xb75a9e30,peer=0x12c360a0,"Signal Catcher"]: reacting to signal 3
07-24 15:55:35.010 2835 2846 I art : Wrote stack traces to '/data/anr/traces.txt'
07-24 15:55:35.010 1932 1973 I Process : Sending signal. PID: 2298 SIG: 3
07-24 15:55:35.010 2298 2312 I art : Thread[2,tid=2312,WaitingInMainSignalCatcherLoop,Thread*=0xb75a9e30,peer=0x12c3e0a0,"Signal Catcher"]: reacting to signal 3
07-24 15:55:35.177 2861 2872 I art : Wrote stack traces to '/data/anr/traces.txt'
07-24 15:55:35.238 2285 2297 I art : Wrote stack traces to '/data/anr/traces.txt'
07-24 15:55:35.362 2298 2312 I art : Wrote stack traces to '/data/anr/traces.txt'
07-24 15:55:36.853 1932 1973 E ActivityManager: ANR in com.ktc.launcher (com.ktc.launcher/.LauncherActivity)
07-24 15:55:36.853 1932 1973 E ActivityManager: PID: 3903
07-24 15:55:36.853 1932 1973 E ActivityManager: Reason: Input dispatching timed out (Waiting to send key event because the focused window has not finished processing all of the input events that were previously delivered to it. Outbound queue length: 0. Wait queue length: 1.)
07-24 15:55:36.853 1932 1973 E ActivityManager: Load: 3.72 / 3.27 / 2.13
07-24 15:55:36.853 1932 1973 E ActivityManager: CPU usage from 543836ms to 0ms ago:
07-24 15:55:36.853 1932 1973 E ActivityManager: 4.5% 1221/tvos: 1.3% user + 3.1% kernel / faults: 6 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.7% 1932/system_server: 1% user + 0.7% kernel / faults: 147841 minor 11 major
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.5% 1138/surfaceflinger: 0.8% user + 0.7% kernel / faults: 359 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.3% 688/irq/202-scaler: 0% user + 0.3% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.3% 1077/logd: 0.2% user + 0% kernel / faults: 58 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.2% 3177/com.android.tv.settings: 0.1% user + 0% kernel / faults: 2048 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.2% 1181/mediaserver: 0.1% user + 0% kernel / faults: 95 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.2% 2285/com.android.systemui: 0.1% user + 0% kernel / faults: 3465 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.1% 1225/virtualkeypad: 0.1% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.1% 1226/livecap: 0% user + 0.1% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.1% 1165/debuggerd: 0% user + 0.1% kernel / faults: 8173 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0.1% 2298/com.mstar.tv.service: 0% user + 0% kernel / faults: 893 minor 2 major
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 2861/com.android.phone: 0% user + 0% kernel / faults: 790 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 292/ion_system_heap: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 3214/com.android.mcast: 0% user + 0% kernel / faults: 12 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 2848/com.mstar.android.pppoe: 0% user + 0% kernel / faults: 489 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 674/mstar sdio hotp: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 8/rcu_preempt: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 2835/com.mediatek.autopair: 0% user + 0% kernel / faults: 432 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 2261/android.process.media: 0% user + 0% kernel / faults: 349 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 2813/com.android.inputmethod.latin: 0% user + 0% kernel / faults: 3 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 6/kworker/u4:0: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 632/mmcqd/0: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 702/T_sensor_Check: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 17/kworker/u4:1: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1104/kworker/0:2: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 23/kworker/1:1: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1261/zygote: 0% user + 0% kernel / faults: 2286 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 2380/android.systemupdate.service: 0% user + 0% kernel / faults: 175 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 2989/com.mstar.android.providers.tv: 0% user + 0% kernel / faults: 1150 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1//init: 0% user + 0% kernel / faults: 117 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 22/kworker/0:1: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1087/kworker/u5:0: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1137/servicemanager: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1159/netd: 0% user + 0% kernel / faults: 5 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 3334/sh: 0% user + 0% kernel / faults: 110 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 3/ksoftirqd/0: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 12/ksoftirqd/1: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 757/ueventd: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1067/jbd2/mmcblk0p10: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1105/vold: 0% user + 0% kernel / faults: 3 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 1133/lmkd: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 0% 3036/com.jrm.localmm: 0% user + 0% kernel / faults: 18 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: +0% 3903/com.ktc.launcher: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: +0% 3944/com.mstar.tv.tvplayer.ui: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: +0% 3986/logcat: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: +0% 4018/kworker/u4:2: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: +0% 4507/cfg80211: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: +0% 4556/RTW_CMD_THREAD: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: +0% 4558/sh: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: +0% 4561/wpa_supplicant: 0% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 5.5% TOTAL: 2.3% user + 2.6% kernel + 0.5% iowait + 0% softirq
07-24 15:55:36.853 1932 1973 E ActivityManager: CPU usage from 3884ms to 4414ms later:
07-24 15:55:36.853 1932 1973 E ActivityManager: 15% 1932/system_server: 9.4% user + 5.6% kernel / faults: 5851 minor
07-24 15:55:36.853 1932 1973 E ActivityManager: 3.7% 1973/ActivityManager: 1.8% user + 1.8% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 3.7% 2178/InputReader: 3.7% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.8% 1940/FinalizerDaemon: 1.8% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.8% 1942/HeapTaskDaemon: 1.8% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 4.7% 1221/tvos: 0% user + 4.7% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 4.7% 1545/InitHDMIthread: 0% user + 4.7% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.5% 1478/IRQThread_213: 0% user + 1.5% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.5% 1617/1221Linux hotpl: 0% user + 1.5% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.5% 1621/1221Linux Input: 0% user + 1.5% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.5% 1717/AudioMonitor: 0% user + 1.5% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.5% 1736/Picture Monitor: 1.5% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.5% 1769/EventManager Di: 1.5% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 1.5% 1226/livecap: 1.5% user + 0% kernel
07-24 15:55:36.853 1932 1973 E ActivityManager: 11% TOTAL: 5.6% user + 5.6% kernel
07-24 15:55:36.877 1932 2177 D WindowManager: keyCode=299 down=false
07-24 15:55:36.877 2298 2325 I CECManager_JNI: getCecConfiguration
07-24 15:55:36.878 1932 1973 W ActivityManager: Launch timeout has expired, giving up wake lock!
07-24 15:55:36.879 1932 4595 D DhcpClient: Broadcasting DHCPDISCOVER
四、traces.txt 文件分析 , 进入系统把 /data/anr/traces.txt 拷贝出来;
从步骤三可以得出是由于com.ktc.launcher/.LauncherActivity导致的ANR,所以直接在traces.txt 文件中可以看到
at com.ktc.launcher.LauncherActivity$3.onReceive(LauncherActivity.java:326)也就是 在onReceice()函数326行出了问题
"main" prio=5 tid=1 Sleeping
| group="main" sCount=1 dsCount=0 obj=0x74be83b0 self=0xb74409f8
| sysTid=4728 nice=0 cgrp=default sched=0/0 handle=0xb6fc0b34
| state=S schedstat=( 0 0 0 ) utm=32 stm=12 core=0 HZ=100
| stack=0xbe24f000-0xbe251000 stackSize=8MB
| held mutexes=
at java.lang.Thread.sleep!(Native method)
- sleeping on <0x0ffd3678> (a java.lang.Object)
at java.lang.Thread.sleep(Thread.java:1031)
- locked <0x0ffd3678> (a java.lang.Object)
at java.lang.Thread.sleep(Thread.java:985)
at com.ktc.launcher.LauncherActivity$3.onReceive(LauncherActivity.java:326)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:881)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke!(Native method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
五、ANR问题的解决,
找到代码的根源,是因为在广播里面执行了耗时的操作,导致了Input dispatching timed out