如果在规定的时间内完成处理,则会调用AMS的serviceDoneExecuting:
//AMS public void serviceDoneExecuting(IBinder token, int type, int startId, int res) { synchronized(this) { if (!(token instanceof ServiceRecord)) { Slog.e(TAG, "serviceDoneExecuting: Invalid service token=" + token); throw new IllegalArgumentException("Invalid service token"); } mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); } }这个方法调用ActiveServices中的serviceDoneExecutingLocked方法:
//ActiveServices void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) { //... serviceDoneExecutingLocked(r, inDestroying, inDestroying); //... }这个方法会调用另一个重载方法:
//ActiveServices private void serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying, boolean finishing) { //... mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app); //... }在这个方法里移除了前面的延时消息,就不会触发ANR。
如果没有及时移除这个消息,那么将会在ActivityManagerService的MainHandler中触发:
MainHandler收到消息的处理:
//ActivityManagerService$MainHandler.handleMessage @Override public void handleMessage(Message msg) { switch (msg.what) { //... case SERVICE_TIMEOUT_MSG: { mServices.serviceTimeout((ProcessRecord)msg.obj); } break; //... } }这个方法调用ActiveServices中的serviceTimeout方法:
void serviceTimeout(ProcessRecord proc) { //... if (anrMessage != null) { mAm.mAppErrors.appNotResponding(proc, null, null, false, anrMessage); } }在这里弹出了不响应的界面。