Android Launcher启动流程

Launcher App 由SystemServer启动,而SystemServer由Zygote进程孵化出来的。

Zygote是孵化器,所有其他Dalvik/ART虚拟机进程都是用过zygote孵化(fock)出来的
SystemServer进程是Android系统的核心之一,大部分Android提供的服务都在该进程中,主要包括ActivityManagerService(AMS)、WindowManagerService(WMS)、PackageManagerSerice(PMS)等。

来看zygote-> SystemServer 入口:
位于frameworks/base/services/java/com/android/server/SystemServer.java

public static void main(String[] args) {
    new SystemServer().run();
}

可以看到,这里main方法调用了run方法

private void run() {
	//...
	// Start services.
    traceBeginAndSlog("StartServices");
    startBootstrapServices(); //启动引导服务
    startCoreServices(); //启动核心服务
    startOtherServices(); //启动其他服务
    //...
}

来看startOtherServices,主要来看SystemServer的systemReady方法,这是Launcher启动的唯一入口。这个方法的作用是只有AMS启动完毕后,才会去回调Runnable中的代码。

private void startOtherServices(){
	//...
	mActivityManagerService.systemReady(() -> {
		//只有ActivityManagerService启动完之后,才会去启动回调里的这些服务
	    Slog.i(TAG, "Making services ready");
	    mSystemServiceManager.startBootPhase(
	            SystemService.PHASE_ACTIVITY_MANAGER_READY);
	    //...
    }
    //...
}

来看ActivityManagerService.systemReady()

public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
	//...
	mStackSupervisor.resumeFocusedStackTopActivityLocked();
	//...
}

再来看ActivityStackSupervisor.resumeFocusedStackTopActivityLocked

boolean resumeFocusedStackTopActivityLocked(
            ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
	//...
    final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
    if (r == null || r.state != RESUMED) {
        mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
    } else if (r.state == RESUMED) {
        mFocusedStack.executeAppTransition(targetOptions);
    }

    return false;
}

再来看ActivityStack.resumeTopActivityUncheckedLocked

boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
        //...
        boolean result = false;
        try {
            mStackSupervisor.inResumeTopActivity = true;
            result = resumeTopActivityInnerLocked(prev, options);
        } finally {
            mStackSupervisor.inResumeTopActivity = false;
        }
		//...
        return result;
    }

再来看resumeTopActivityInnerLocked

private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
     //...
     if (!hasRunningActivity) {
         return resumeTopActivityInNextFocusableStack(prev, options, "noMoreActivities");
     }
     //...
}

接着来看resumeTopActivityInNextFocusableStack

private boolean resumeTopActivityInNextFocusableStack(ActivityRecord prev,
            ActivityOptions options, String reason) {
	//...
	// Only resume home if on home display
 	return isOnHomeDisplay() &&
          mStackSupervisor.resumeHomeStackTask(prev, reason);
}

这里的mStackSupervisor.resumeHomeStackTask就是真正启动Launcher App的入口了

boolean resumeHomeStackTask(ActivityRecord prev, String reason) {
    //...
    return mService.startHomeActivityLocked(mCurrentUser, myReason);
}

这里会通过ActivityManagerService.startHomeActivityLocked

来看ActivityManagerService

boolean startHomeActivityLocked(int userId, String reason) {
        //...
        Intent intent = getHomeIntent();
        ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
        if (aInfo != null) {
        	//创建Launcher启动所需的Intent
            intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
            // Don't do this if the home app is currently being
            // instrumented.
            aInfo = new ActivityInfo(aInfo);
            aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
            ProcessRecord app = getProcessRecordLocked(aInfo.processName,
                    aInfo.applicationInfo.uid, true);
            if (app == null || app.instr == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                final int resolvedUserId = UserHandle.getUserId(aInfo.applicationInfo.uid);
                // For ANR debugging to verify if the user activity is the one that actually
                // launched.
                final String myReason = reason + ":" + userId + ":" + resolvedUserId;
                //启动Launcher
                mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason);
            }
        } else {
            Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
        }

        return true;
    }

可以看到,这里通过Intent,调用mActivityStarter.startHomeActivityLocked

来看ActivityStarter

void startHomeActivityLocked(Intent intent, ActivityInfo aInfo, String reason) {
		//将Launcher放入HomeStack中,HomeStack是在ActivityManagerService中定义的用于存储Launcher的变量
        mSupervisor.moveHomeStackTaskToTop(reason);
        mLastHomeActivityStartResult = startActivityLocked(null /*caller*/, intent,
                null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
                null /*voiceSession*/, null /*voiceInteractor*/, null /*resultTo*/,
                null /*resultWho*/, 0 /*requestCode*/, 0 /*callingPid*/, 0 /*callingUid*/,
                null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/,
                0 /*startFlags*/, null /*options*/, false /*ignoreTargetSecurity*/,
                false /*componentSpecified*/, mLastHomeActivityStartRecord /*outActivity*/,
                null /*inTask*/, "startHomeActivity: " + reason);
        if (mSupervisor.inResumeTopActivity) {
            // If we are in resume section already, home activity will be initialized, but not
            // resumed (to avoid recursive resume) and will stay that way until something pokes it
            // again. We need to schedule another resume.
            mSupervisor.scheduleResumeTopActivities();
        }
    }

在这里就会去Manifest中寻找Launcher应用,并启动Launcher应用,接下来的启动过程就和Activity的启动过程类似了。
在这里插入图片描述

其他

参考
Zygote进程浅析
SystemServer进程浅析
Init进程源码分析
开机SystemServer到ActivityManagerService启动过程分析

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

氦客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值