Android Activity组件与进程启动间关系

Activity、Service、ContentProvider、BroadcastReceiver这四大组件,在启动的过程中,当其承载的进程不存在时需要调用startProcessLocked先创建进程。

下面以launcher activtity的启动流程为例

java 复制代码
@frameworks/base/services/core/java/com/android/server/wm/ActivityStartController.java
                                                    Service.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason, taskDisplayArea);
@frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java
                                                        mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason).setActivityOptions(options.toBundle()).execute();
                                                            res = executeRequest(mRequest);
                                                                ActivityRecord r = new ActivityRecord.Builder(mService)..setActivityInfo(aInfo)
                                                                mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
                                                                    result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,startFlags, doResume, options, inTask, inTaskFragment, restrictedBgActivity,
                                                                        mTargetRootTask.startActivityLocked(mStartActivity,
@frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java
                                                                        mRootWindowContainer.resumeFocusedTasksTopActivities(mTargetRootTask, mStartActivity, mOptions, mTransientLaunch);
                                                                            final Task focusedRoot = display.getFocusedRootTask();
@frameworks/base/services/core/java/com/android/server/wm/Task.java
                                                                            focusedRoot.resumeTopActivityUncheckedLocked(target, targetOptions);
                                                                                someActivityResumed = resumeTopActivityInnerLocked(prev, options, deferPause);
@frameworks/base/services/core/java/com/android/server/wm/TaskFragment.java
                                                                                    resumed[0] = topFragment.resumeTopActivity(prev, options, deferPause);
                                                                                        ActivityRecord next = topRunningActivity(true /* focusableOnly */);
                                                                                        pausing |= startPausing(mTaskSupervisor.mUserLeaving, false /* uiSleeping */, next, "resumeTopActivity");  //这里就是stop上一个activity的地方
                                                                                        if (next.attachedToProcess()) {   //要启动的activity所在的进程已经起来了,直接通过事务的形式去启动activity
                                                                                            final ClientTransaction transaction = ClientTransaction.obtain(next.app.getThread(), next.appToken);
                                                                                            mAtmService.getLifecycleManager().scheduleTransaction(transaction);
                                                                                        } else {   //如果进程没启动,调用ActivityTaskSupervisor的startSpecificActivity
@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java
                                                                                            mTaskSupervisor.startSpecificActivity(next, true, true);
@base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
                                                                                                mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity");
                                                                                                    final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess, mAmInternal, activity.processName,
                                                                                                    mH.sendMessage(m);
                                                                                                        LocalService::startProcess  //class LocalService extends ActivityManagerInternal
                                                                                                            startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */, new HostingRecord(hostingType, hostingName, isTop),
@frameworks/base/services/core/java/com/android/server/am/ProcessList.java
                                                                                                                return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
                                                                                                                    startProcessLocked(app, hostingRecord, zygotePolicyFlags, abiOverride);
                                                                                                                        startProcessLocked(app, hostingRecord, zygotePolicyFlags, false
                                                                                                                            startProcessLocked(hostingRecord, entryPoint, app, uid, gids, runtimeFlags, zygotePolicyFlags, mountExternal, seInfo, requiredAbi,
                                                                                                                                mService.mProcStartHandler.post(() -> handleProcessStart( app,
                                                                                                                                    handleProcessStartedLocked(app, startResult, startSeq);
                                                                                                                                        return handleProcessStartedLocked(pending, startResult.pid, startResult.usingWrapper,
                                                                                                                                            buf.append("Start proc ");  //Start proc 1486:com.android.launcher/1000 for top-activity {com.android.launcher/com.android.launcher.MainActivity}
                                                                                                                                            buf.append(pid);
                                                                                                                                            buf.append(':');
                                                                                                                                            buf.append(app.processName);
                                                                                                                                            buf.append('/');
                                                                                                                                            buf.append(app.getHostingRecord().getType());
                                                                                                                                            buf.append(app.getHostingRecord().getName());
                                                                                                                                            
                                                                                                                                            ..............
@src/main/java/com/dynabook/launcher/MainActivity.java
                                                                                                                                                MainActivity::onCreate 

@frameworks/base/services/core/java/com/android/server/wm/ActivityStartController.java

Service.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason, taskDisplayArea);

@frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java

mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason).setActivityOptions(options.toBundle()).execute();

res = executeRequest(mRequest);

ActivityRecord r = new ActivityRecord.Builder(mService)..setActivityInfo(aInfo)

mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,

result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,startFlags, doResume, options, inTask, inTaskFragment, restrictedBgActivity,

mTargetRootTask.startActivityLocked(mStartActivity,

@frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java

mRootWindowContainer.resumeFocusedTasksTopActivities(mTargetRootTask, mStartActivity, mOptions, mTransientLaunch);

final Task focusedRoot = display.getFocusedRootTask();

@frameworks/base/services/core/java/com/android/server/wm/Task.java

focusedRoot.resumeTopActivityUncheckedLocked(target, targetOptions);

someActivityResumed = resumeTopActivityInnerLocked(prev, options, deferPause);

@frameworks/base/services/core/java/com/android/server/wm/TaskFragment.java

resumed[0] = topFragment.resumeTopActivity(prev, options, deferPause);

ActivityRecord next = topRunningActivity(true /* focusableOnly */);

pausing |= startPausing(mTaskSupervisor.mUserLeaving, false /* uiSleeping */, next, "resumeTopActivity"); //这里就是stop上一个activity的地方

if (next.attachedToProcess()) { //要启动的activity所在的进程已经起来了,直接通过事务的形式去启动activity

final ClientTransaction transaction = ClientTransaction.obtain(next.app.getThread(), next.appToken);

mAtmService.getLifecycleManager().scheduleTransaction(transaction);

} else { //如果进程没启动,调用ActivityTaskSupervisor的startSpecificActivity

@frameworks/base/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java

mTaskSupervisor.startSpecificActivity(next, true, true);

@base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity");

final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess, mAmInternal, activity.processName,

mH.sendMessage(m);

LocalService::startProcess //class LocalService extends ActivityManagerInternal

startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */, new HostingRecord(hostingType, hostingName, isTop),

@frameworks/base/services/core/java/com/android/server/am/ProcessList.java

return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,

startProcessLocked(app, hostingRecord, zygotePolicyFlags, abiOverride);

startProcessLocked(app, hostingRecord, zygotePolicyFlags, false

startProcessLocked(hostingRecord, entryPoint, app, uid, gids, runtimeFlags, zygotePolicyFlags, mountExternal, seInfo, requiredAbi,

mService.mProcStartHandler.post(() -> handleProcessStart( app,

handleProcessStartedLocked(app, startResult, startSeq);

return handleProcessStartedLocked(pending, startResult.pid, startResult.usingWrapper,

buf.append("Start proc "); //Start proc 1486:com.android.launcher/1000 for top-activity {com.android.launcher/com.android.launcher.MainActivity}

buf.append(pid);

buf.append(':');

buf.append(app.processName);

buf.append('/');

buf.append(app.getHostingRecord().getType());

buf.append(app.getHostingRecord().getName());

..............

@src/main/java/com/dynabook/launcher/MainActivity.java

MainActivity::onCreate

相关推荐
吾即是光6 分钟前
[SWPUCTF 2021 新生赛]error
android
大耳猫22 分钟前
Android 基于Camera2 API进行摄像机图像预览
android·kotlin·相机·camera
MYBOYER25 分钟前
Kotlin DSL Gradle 指南
android·开发语言·kotlin
Mr_Xuhhh1 小时前
程序地址空间
android·java·开发语言·数据库
呆呆小雅3 小时前
C# 结构体
android·java·c#
ᥬ 小月亮5 小时前
Layui表格的分页下拉框新增“全部”选项
android·javascript·layui
sunly_14 小时前
Flutter:启动屏逻辑处理02:启动页
android·javascript·flutter
Sgq丶15 小时前
Android Studio 配置 proto
android·ide·android studio
_小马快跑_19 小时前
ConstraintLayout 中的ImageFilterView探索:处理图片圆角、亮度、饱和度、图片重叠等
android
IT-sec19 小时前
jquery-picture-cut 任意文件上传(CVE-2018-9208)
android·前端·javascript·安全·web安全·网络安全·jquery