ANR:Application Not Responding,应用无响应,在一定时间内,用户的操作没有给出回应,就会导致ANR。
首先要明确一个概念:ANR只会出现在主线程中,也就是说Activity、Service、BroadcastReceiver、ContentProvider的生命周期方法都是在主线程中执行的,系统通过AMS和WMS对操作时间进行检测,超出时间就会报ANR,并且弹出对话框提示用户。
那是什么机制对方法执行时间进行检测呢,这个地方就涉及了Android消息机制的Looper对象了,内部有一个死循环,等待消息然后进行处理。
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
....
}
这里就会存在问题,为什么Looper.loop()不会导致ANR,是因为loop()方法是为了维持当前app一直存活的,如果没有loop()方法,那么app处理完消息就直接退出应用了。ANR导致的原因是loop()中取出的某一条消息没有在规定的时间内处理完,才会出现的。
loop()方法的完整流程是:从queue中取出所有消息,进行处理;当没有消息的时候,queue.next()内部也是一个无限循环会阻塞主线程,主线程进入休眠状态;当再次发送消息到queue中的时候,就会取出消息发送到loop()方法内部,唤醒主线程进行处理;处理完继续阻塞,进入休眠状态。
参考:Android面试:主线程中的Looper.loop()一直无限循环为什么不会造成ANR?
以下代码都是基于 Android 10.0(29),其他版本的代码位置需要自己去查找了
ActivityManagerService存放广播和内容提供者最大执行时间
// How long we allow a receiver to run before giving up on it.
static final int BROADCAST_FG_TIMEOUT = 10*1000; //前台广播接收者方法操作时间
static final int BROADCAST_BG_TIMEOUT = 60*1000;//后台广播接收者方法操作时间
// How long we wait for an attached process to publish its content providers
// before we decide it must be hung.
static final int CONTENT_PROVIDER_PUBLISH_TIMEOUT = 10*1000;//ContentProvider的publish执行时间
/**
* How long we wait for an provider to be published. Should be longer than
* {@link #CONTENT_PROVIDER_PUBLISH_TIMEOUT}.
*/
static final int CONTENT_PROVIDER_WAIT_TIMEOUT = 20 * 1000;
WindowManagerService存放动画最大执行时间定义
/** The maximum length we will accept for a loaded animation duration:
* this is 10 seconds.
*/
static final int MAX_ANIMATION_DURATION = 10 * 1000; //动画最大执行时间
/** Amount of time (in milliseconds) to delay before declaring a window freeze timeout. */
static final int WINDOW_FREEZE_TIMEOUT_DURATION = 2000; //界面冻结时间
/** Amount of time (in milliseconds) to delay before declaring a seamless rotation timeout. */
static final int SEAMLESS_ROTATION_TIMEOUT_DURATION = 2000;
/** Amount of time (in milliseconds) to delay before declaring a window replacement timeout. */
static final int WINDOW_REPLACEMENT_TIMEOUT_DURATION = 2000;
在ActivityServices类中存放Service超时时间定义
// How long we wait for a service to finish executing.
static final int SERVICE_TIMEOUT = 20*1000;
// How long we wait for a service to finish executing.
static final int SERVICE_BACKGROUND_TIMEOUT = SERVICE_TIMEOUT * 10;
ActivityTaskManagerService类中存放输入事件超时时间定义
// How long we wait until we timeout on key dispatching.
public static final int KEY_DISPATCHING_TIMEOUT_MS = 5 * 1000;
// How long we wait until we timeout on key dispatching during instrumentation.
static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT_MS = 60 * 1000;
// How long we permit background activity starts after an activity in the process
// started or finished.
static final long ACTIVITY_BG_START_GRACE_PERIOD_MS = 10 * 1000;
上面都是具体的时间定义,至于过程就暂时不分析了。
有了上面的定义值,我们就知道该怎么操作了:
1、避免在四大组件的生命周期方法中做耗时的操作;
2、当然还要避免主线程因为被其他线程锁住;
3、当前app进程没有获取到足够的资源无法执行主线程。
如果出现的anr,正常的在Logcat中都是能找到原因的,如果实在找不到可以到处anr对应的文件,trace文件保存到了/data/anr/traces.txt中,可以通过adb将文件导出来,然后进行分析。一般搜索自己的项目包名就可以找到自己的出问题的代码位置。
adb pull /data/anr

ANR是指Application Not Responding,当主线程在特定时间内未响应用户操作时发生。ANR通常与主线程中耗时操作有关,涉及Looper.loop()的无限循环。为避免ANR,应避免在四大组件的生命周期方法中执行长时间任务,防止主线程被其他线程阻塞,确保应用程序获得足够资源。当出现ANR时,可通过Logcat或分析/data/anr/traces.txt文件找出问题所在。
346





