ActivityThread 的main (after fork)方法会通过IPC调用到 ActivityManagerService 的 attachApplication(mAppThread, startSeq) 方法, 然后
com.android.server.am.ActivityManagerService#attachApplication 调用
com.android.server.am.ActivityManagerService#attachApplicationLocked 调用
com.android.server.wm.ActivityTaskManagerService.LocalService#attachApplication 调用
com.android.server.wm.RootWindowContainer#attachApplication调用
com.android.server.wm.RootWindowContainer#startActivityForAttachedApplicationIfNeeded 调用
com.android.server.wm.ActivityStackSupervisor#realStartActivityLocked 调用
com.android.server.wm.ClientLifecycleManager#scheduleTransaction 调用
android.app.servertransaction.ClientTransaction#schedule 然后内部实现是 :
ApplicationThread#scheduleTransaction ,由于ApplicationThread是一个Binder对象, 这样就跨进程 调用到了目标进程,
ApplicationThread 是 ClientTransactionHandler的子类 ,
最终调用 android.app.ClientTransactionHandler#scheduleTransaction 方法, 发送 了 EXECUTE_TRANSACTION 信息 给 ApplicationThread 的 H,然后到
android.app.servertransaction.TransactionExecutor#executeCallbacks 然后到
android.app.servertransaction.ActivityLifecycleItem 的 execute ,
如果是启动activity 。那么 ActivityLifecycleItem 的实际对象是 android.app.servertransaction.StartActivityItem,所以实际调用的是
android.app.servertransaction.StartActivityItem#execute , 其内容如下:
@Override
public void execute(ClientTransactionHandler client, IBinder token,
PendingTransactionActions pendingActions) {
Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "startActivityItem");
client.handleStartActivity(token, pendingActions);
Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
}
这个 client 就是 ActivityThread ,这样就调用到了 ActivityThread 的 handleStartActivity 方法当中 。
com.android.server.am.ActivityManagerService#attachApplicationLocked 方法用来判断启动的是一个
一个Activity ,还是service 或者 broadcast 。
也可以查看以下堆栈信息。
2023-01-17 20:34:07.723 433-480/system_process E/ActivityManager: Exception thrown launching activities in ProcessRecord{c1c8fd4 11910:com.example.test2/u0a124}
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at com.android.server.wm.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:61)
at com.android.server.wm.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:882)
at com.android.server.wm.RootWindowContainer.startActivityForAttachedApplicationIfNeeded(RootWindowContainer.java:2005)
at com.android.server.wm.RootWindowContainer.lambda$5fbF65VSmaJkPHxEhceOGTat7JE(Unknown Source:0)
at com.android.server.wm.-$$Lambda$RootWindowContainer$5fbF65VSmaJkPHxEhceOGTat7JE.apply(Unknown Source:8)
at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:315)
at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:201)
at com.android.internal.util.function.pooled.OmniFunction.apply(OmniFunction.java:78)
at com.android.server.wm.ActivityRecord.forAllActivities(ActivityRecord.java:3642)
at com.android.server.wm.WindowContainer.forAllActivities(WindowContainer.java:1335)
at com.android.server.wm.WindowContainer.forAllActivities(WindowContainer.java:1325)
at com.android.server.wm.RootWindowContainer.attachApplication(RootWindowContainer.java:1958)
at com.android.server.wm.ActivityTaskManagerService$LocalService.attachApplication(ActivityTaskManagerService.java:6906)
at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:5486)
at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:5575)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2336)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2985)
at android.os.Binder.execTransactInternal(Binder.java:1154)
at android.os.Binder.execTransact(Binder.java:1123)