顺便说下,系统进程通过IApplicationThread通知App,由于是Binder线程,App为了切换到UI线程,所以都是经过ActivityThread.H.sendMessage(),但是这样对系统进程来说,就是异步而不是同步了,所以系统进程在通知App后都会给自己设置一个超时处理。
比如系统进程通知App进行pause,App执行成功后会通知系统进程执行activityPausedLocked,但如果App超过500ms(PAUSE_TIMEOUT )还没通知系统进程自己pause成功,那么系统进程也会执行activityPausedLocked,而在activityPausedLocked内就把这个message移除。
这是健硕的程序的一个体现。
static final int LAUNCH_TICK = 500;
static final int PAUSE_TIMEOUT = 500;
static final int STOP_TIMEOUT = 10*1000;
static final int DESTROY_TIMEOUT = 10*1000;
final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
boolean dontWait) {
...
prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing, userLeaving, prev.configChangeFlags, dontWait);
...
Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
msg.obj = prev;
prev.pauseTime = SystemClock.uptimeMillis();
mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
...
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case PAUSE_TIMEOUT_MSG: {
ActivityRecord r = (ActivityRecord)msg.obj;
synchronized (mService) {
activityPausedLocked(r.appToken, true);
}
} break;
...
}
}
final void activityPausedLocked(IBinder token, boolean timeout) {
final ActivityRecord r = isInStackLocked(token);
if (r != null) {
mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
}
...
}