Android system startup process-----activity startup process

Preface

For top-level application developers, many people only know that activity starts from ActivityThread, and do not know the specific calling process of frameworks. Today we will explain it in detail starting from clicking on the launcher icon.
Insert image description here
activity:
startActivity–> startActivityForResult–>mParent.startActivityFromChild–>
(come to Instrumentation) mInstrumentation.execStartActivity–>ActivityManager.getService().startActivity–> (come to windowmanagerService)
windowmanagerService.startActivity–>

1.frameworks/base/core/java/android/app/Activity.java

Click the application icon from luancher, and you will eventually come to the activity's startActivity. After a series of method calls: startActivity–> startActivityForResult–>mParent.startActivityFromChild–>mInstrumentation.execStartActivity

2.frameworks/base/core/java/android/app/Instrumentation.java

Instrumentation.execStartActivity will get the binder proxy class ServiceManagerProxy from ServiceManager. This proxy class will eventually call startActivity in ActivityManagerService. At this point, it will come from the launcher process to the system_Server process try { intent.migrateExtraStreamToClipData()
; intent.prepareToLeaveProcess ( who); int result = ActivityManager.getService() .startActivity(whoThread, who.getBasePackageName(), intent, intent.resolveTypeIfNeeded(who.getContentResolver()), token, target != null ? target.mEmbeddedID : null, requestCode, 0, null, options); checkStartActivityResult(result, intent); } catch (RemoteException e) { throw new RuntimeException(“Failure from system”, e); }










3.frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

ActivityManagerService will first check the user's permissions and whether the process where the activity is located is created. If not, a socket message will be created to notify the zygote process to fork the process.
After the fork is successful, start the activityThread–>main method of the new process by launching it.

4.frameworks/base/core/java/android/app/ActivityThread.java

In the main method of activityThread, bind ApplicationThread to ActivityManagerService through attachApplication.
final IActivityManager mgr = ActivityManager.getService();
try { mgr.attachApplication(mAppThread); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); }



5.frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

public final void attachApplication(IApplicationThread thread) {
synchronized (this) {
int callingPid = Binder.getCallingPid();
final long origId = Binder.clearCallingIdentity();
attachApplicationLocked(thread, callingPid);
Binder.restoreCallingIdentity(origId);
}
}
ActivityManagerService会调用attachApplicationLocked,改方法会通过thread.bindApplication初始化应用
if (app.instr != null) {
thread.bindApplication(processName, appInfo, providers,
app.instr.mClass,
profilerInfo, app.instr.mArguments,
app.instr.mWatcher,
app.instr.mUiAutomationConnection, testMode,
mBinderTransactionTrackingEnabled, enableTrackAllocation,
isRestrictedBackupMode || !normalMode, app.persistent,
new Configuration(getGlobalConfiguration()), app.compat,
getCommonServicesLocked(app.isolated),
mCoreSettingsObserver.getCoreSettingsLocked(),
buildSerial);
} else {
thread.bindApplication(processName, appInfo, providers, null, profilerInfo,
null, null, null, testMode,
mBinderTransactionTrackingEnabled, enableTrackAllocation,
isRestrictedBackupMode || !normalMode, app.persistent,
new Configuration(getGlobalConfiguration()), app.compat,
getCommonServicesLocked(app.isolated),
mCoreSettingsObserver.getCoreSettingsLocked(),
buildSerial);
}
if (normalMode) {
try {
if (mStackSupervisor.attachApplicationLocked(app)) { didSomething = true; } } catch (Exception e) { Slog.wtf(TAG, "Exception thrown launching activities in " + app, e); badApp = true; } } and pass a A series of methods call mStackSupervisor.attachApplicationLocked–> realStartActivityLocked–>app.thread.scheduleLaunchActivity scheduleLaunchActivity is to start the activity, and the application startup is completed.









Guess you like

Origin blog.csdn.net/L779442863/article/details/124884739