源码分析ANR超时

ANR超时

Activity 超时 5秒
ActivityManagerService.java

1
2
// How long we wait until we timeout on key dispatching.
static final int KEY_DISPATCHING_TIMEOUT = 5*1000;

前台service 超时默认20秒 后台service 超时200秒
android framework 源码中 ActiveServices.java

1
2
3
4
5
// 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;
1
2
3
4
5
6
7
8
void serviceTimeout(ProcessRecord proc) {
//...
//proc.execServicesFg 是否前台执行任务

final long maxTime = now -
(proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT);
//...
}

广播,前台 10秒 后台60秒
ActivityManagerService.java

1
2
3
// 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;