Launcher启动流程及初始化

本文详细分析了Android 2.3.7版本中Launcher的启动和初始化过程。从SystemServer的`systemReady()`开始,经过`ActivityManagerService`的`startHomeActivityLocked()`启动Launcher,再到Launcher自身在`onCreate()`中初始化UI,特别是`setupViews()`和`bindItems()`方法对点击事件的处理。在`bindItems()`中,创建快捷方式并设置点击事件,当点击图标时,通过`startActivitySafely()`启动应用。文章强调了Launcher的启动和点击事件初始化与绘制的分离,并预告了后续将分析点击图标后的启动流程。

前言

前面我们学习了SystemServer的启动流程,也了解了AMS是如何启动起来并通过Binder注册到ServiceManger内的,OK,本文基于这俩篇基础继续来学习Launcher。

  • Launcher是如何启动起来的
  • Launcher启动起来之后自身的流程是怎样初始化的

PS:本文的流程分析基于android_2.3.7,高版本的源码和本篇文章流程分析略有出入,请注意自己当前源码的版本。

流程代码分析

1.Launcher是如何启动起来的

流程图

frameworks\base\services\java\com\android\server\SystemServer.java$ServerThread#run()



class ServerThread extends Thread {
    private static final String TAG = "SystemServer";

    ......

    @Override
    public void run() {
        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
            SystemClock.uptimeMillis());

        ......
        ((ActivityManagerService)ActivityManagerNative.getDefault())
                .systemReady(new Runnable() {
            public void run() {
                Slog.i(TAG, "Making services ready");

                if (statusBarF != null) statusBarF.systemReady2();
                if (batteryF != null) batteryF.systemReady();
                if (connectivityF != null) connectivityF.systemReady();
                if (dockF != null) dockF.systemReady();
                if (usbF != null) usbF.systemReady();
                if (uiModeF != null) uiModeF.systemReady();
                if (recognitionF != null) recognitionF.systemReady();
                Watchdog.getInstance().start();

                // It is now okay to let the various system services start their
                // third party code...

                if (appWidgetF != null) appWidgetF.systemReady(safeMode);
                if (wallpaperF != null) wallpaperF.systemReady();
                if (immF != null) immF.systemReady();
                if (locationF != null) locationF.systemReady();
                if (throttleF != null) throttleF.systemReady();
            }
        });

      ......
    }
}

这个getDefault实际上返回的就是Ams。强转之后去调用systemReady()。

frameworks\base\services\java\com\android\server\am\ActivityManagerService.java#systemReady()

    public void systemReady(final Runnable goingCallback) {
        // In the simulator, startRunning will never have been called, which
        // normally sets a few crucial variables. Do it here instead.
         ......

        synchronized(this) {
         ......

            mMainStack.resumeTopActivityLocked(null);
        }
    }

systemReady内调用了mMainStack的resumeTopActivityLocked方法,点进去。
延伸思考:ActivityStack mMainStack是什么?

frameworks\base\services\java\com\android\server\am\ActivityStack.java#resumeTopActivityLocked()


    /**
     * Ensure that the top activity in the stack is resumed.
     *
     * @param prev The previously resumed activity, for when in the process
     * of pausing; can be null to call from elsewhere.
     *
     * @return Returns true if something is being resumed, or false if
     * nothing happened.
     */
    final boolean resumeTopActivityLocked(ActivityRecord prev) {
        // Find the first activity that is not finishing.
        ActivityRecord next = topRunningActivityLocked(null);

        // Remember how we'll process this pause/resume situation, and ensure
        // that the state is reset however we wind up proceeding.
        final boolean userLeaving = mUserLeaving;
        mUserLeaving = false;

        if (next == null) {
            // There are no more activities!  Let's just start up the
            // Launcher...
            if (mMainStack) {
                return mService.startHomeActivityLocked();
            }
        }

        next.delayedResume = false;

        ......

        return true;
    }

ActivityStack.javaresumeTopActivityLocked()方法内又去调用了mService.startHomeActivityLocked(),这个mService就是ActivityManagerService
那么再回到ActivityManagerService去看startHomeActivityLocked()

frameworks\base\core\java\android\app\ActivityManagerService.java#startHomeActivityLocked()

  boolean startHomeActivityLocked() {

        if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
                && mTopAction == null) {
            // We are running in factory test mode, but unable to find
            // the factory test app, so just sit around displaying the
            // error message and don't try to start anything.
            return false;
        }

        Intent intent = new Intent(
            mTopAction,
            mTopData != null ? Uri.parse(mTopData) : null);

        intent.setComponent(mTopComponent);

        if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
            intent.addCategory(Intent.CATEGORY_HOME);
        }

        //向PKMS查询满足条件的ActivityInfo
        ActivityInfo aInfo =
            intent.resolveActivityInfo(mContext.getPackageManager(),
                    STOCK_PM_FLAGS);

        if (aInfo != null) {
            intent.setComponent(new ComponentName(
                    aInfo.applicationInfo.packageName, aInfo.name));
            // Don't do this if the home app is currently being
            // instrumented.
            //在正常情况下,app应该为null,因为刚开机,Home进程肯定还没启动
            ProcessRecord app = getProcessRecordLocked(aInfo.processName,
                    aInfo.applicationInfo.uid);

            if (app == null || app.instrumentationClass == null) {
                //启动Home
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                mMainStack.startActivityLocked(null, intent, null, null
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值