一 . 什么是ANR?
答:ANR:Application Not Responding(应用程序无响应)
二 . 哪些场景会触发ANR(或ANR类型有哪些)?
答:1.InputEventDispatching Timeout:输入事件分发超过5s(包括按键和触摸事件);
2.Service Timeout:前台服务超过20s;后台服务超过200s;
3.Broadcast Timeout:前台广播超过10s;后台广播超过60s;
4.ContentProvider Timeout:内容提供者超过10s;
三.如何快速定位ANR(阿里巴巴面试题)?
答:使用adb命令: adb pull data/anr/traces.txt ; traces.txt 会保存到当前所在的目录下,从trace.txt文件查看调用stack;
四.深入理解触发ANR的过程:
实质是埋炸弹,拆炸弹,引爆炸弹的过程
1.InputEventDispatching Timeout:
当发生ANR时system log中会出现以下信息, 并且TAG=WindowManager,
Input event dispatching timed out xxx. Reason: + reason, 其中xxx取值:
窗口类型: sending to windowState.mAttrs.getTitle()
应用类型: sending to application appWindowToken.stringName
其他类型: 则为空.
reason类型:
无窗口, 有应用:Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.
窗口暂停: Waiting because the [targetType] window is paused.
窗口未连接: Waiting because the [targetType] window’s input channel is not registered with the input dispatcher. The window may be in the process of being removed.
窗口连接已死亡:Waiting because the [targetType] window’s input connection is [Connection.Status]. The window may be in the process of being removed.
窗口连接已满:Waiting because the [targetType] window’s input channel is full. Outbound queue length: [outboundQueue长度]. Wait queue length: [waitQueue长度].
按键事件,输出队列或事件等待队列不为空:Waiting to send key event because the [targetType]window has not finished processing all of the input events that were previously delivered to it. Outbound queue length: [outboundQueue长度]. Wait queue length: [waitQueue长度].
非按键事件,事件等待队列不为空且头事件分发超时500ms:Waiting to send non-key event because the [targetType] window has not finished processing certain input events that were delivered to it over 500ms ago. Wait queue length: [waitQueue长度]. Wait queue head age: [等待时长].
未完待续.....