1、ANR介绍
ANR(Application Not Responding)指的是应用程序无响应,当Android应用程序在主线程上执行长时间运行的操作或阻塞I/O操作时发生。这可能导致应用程序界面冻结或无法响应用户输入。
1、Service ANR:前台20s,后台200s;startForeground超时10s
2、BroadcastReceiver ANR:前台10s,后台60s
3、Input ANR:按键或触摸事件在5s内无响应
4、ContentProvider ANR:10s,少见
2、抓取离线日志

使用CAT或QATT解析dbg文件


解析完成相关Log如下

3、Service ANR
Service ANR原理,当框架启动APP对应的Service时。一边在框架自身发起一个handler超时消息,一边发送启动Service 的handler消息到APP的主线程。当APP Service中的onCreate方法调用后,会回调框架中的serviceDoneExecutingLocked来取消定超时消息。正常情况下如此

如果是APP本身问题,可以通过SYS_FTRACE搜索serviceCreate来确定执行时长。

同步其他Log进行分析确认。

执行超时消息,触发ANR
1、若binder耗时(比较少见);
2、app主线程执行一些消息耗时,导致service相关的消息得不到执行;
3、Service本身的周期方法onCreate,onStartCommand等执行一些耗时操作;
4、BroacastReceiver 超时
Android中有两种广播机制:
1、静态广播接收者:通过AndroidManifest.xml的标签来申明的BroadcastReceiver。
2、动态广播接收者:通过Context.registerReceiver()方式注册的BroadcastReceiver,动态注册更为灵活,可在不需要时通过unregisterReceiver()取消注册。
从广播发送方式可分为三类:
1、普通广播:通过Context.sendBroadcast()发送,可并行处理
2、有序广播:通过Context.sendOrderedBroadcast()发送,串行处理
3、Sticky广播:通过Context.sendStickyBroadcast()发送
其中前台广播ANR 10s,后台广播ANR 60s


发送广播的逻辑主要在框架中的BroadcastQueue的函数processNextBroadcastLocked中。对于并行广播mParallelBroadcasts直接全部发送。对于有序广播,每次设置超时时间,发送一条。APP接收到关播后,调用finish来进行下一条循环。

如果是APP本身问题,可以通过SYS_FTRACE搜索broadcastReceiveComp来确定执行时长。


5、Input超时

检查 Relayout windows时间点(此时 activity 能被看到)。如果发 生 ANR 时,Focus焦点已经切换过来,则需要APP分析,否则需要系统侧进一步分析。

其中绘制过程比较复杂,相关流程整理如下,后续更新

6、 ContentProvider 超时
后续补充
7、其他相关信息确认
很多时候,ANR不一定是APP本身的问题,有可能是系统卡顿或者IO问题导致的。需要结合相关ANR Log中的其他信息一起分析。
1、CPU的使用率

2、剩余内存大小

EVENT Log中搜killinfo

参考文档