Android 应用启动过程优化

应用启动流程:

1、startActivity 交给AMS判断处理(Binder通信)

2、AMS匹配到对应的应用信息后通知zygote去fork进程(socket通信)

3、反射调用ActivityThreadd的main函数之后,将匿名binder(ApplicationThread)交由AMS,建立了app的binder通信基础。

bash 复制代码
 final IActivityManager mgr = ActivityManager.getService();
            try {
                mgr.attachApplication(mAppThread, startSeq);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }

4、AMS通知App进程(binder通信)去创建启动Activity(onCreate...)

attachBaseContext阶段

https://github.com/bytedance/BoostMultiDex替代google的dex分包

Application的onCreate阶段

将优先级低的组件延后加载

主线程

减少耗时操作

优先级低的操作放在IdleHandler中去执行

Provider

该组件是在Applicaiton创建完成之后,立即installProvider,该过程可以通过懒加载模式进行优化

View

减少View层级绘制

黑白屏

theme设置windowbackground,换成应用的LOGO。

问题来了,我们知道Activity内有Window对象。Window对象中的View,在Onresume(addView)后才会绘制完成,我们才能看到界面。

这个界面是在startActivityLocked中会调用

bash 复制代码
 r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));

来添加一个Window,这个线程不是主线程,开了一个动画线程来展示

PhoneWindowManager.java

bash 复制代码
 /** {@inheritDoc} */
    @Override
    public StartingSurface addSplashScreen(IBinder appToken, int userId, String packageName,
            int theme, CompatibilityInfo compatInfo, CharSequence nonLocalizedLabel, int labelRes,
            int icon, int logo, int windowFlags, Configuration overrideConfig, int displayId) {
       .......................

            params.setTitle("Splash Screen " + packageName);
            addSplashscreenContent(win, context);

            wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
            view = win.getDecorView();

            if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "Adding splash screen window for "
                + packageName + " / " + appToken + ": " + (view.getParent() != null ? view : null));

            wm.addView(view, params);
	........................
    }
相关推荐
我就是妖怪4 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·人工智能
心平气和量大福大5 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
阿pin7 小时前
Android随笔-Serializable与Parcelable
android·serializable·parcelable
天空之城--7 小时前
Android 事件分发机制深度解析——原理、源码与实战综合总结
android
阿pin8 小时前
Android随笔-AIDL
android·开发语言·aidl
2501_916007479 小时前
Bundle ID 注册与管理 App ID 创建指南 命名规则 权限开关与删除注意事项
android·ios·小程序·https·uni-app·iphone·webview
雨白9 小时前
面向对象六大基本原则实战:从零重构支持引擎切换的网络框架
android·架构
张赐荣10 小时前
Android TalkBack 无障碍优化: 为控件自定义转子导航动作
android·microsoft
天空之城--12 小时前
Android Flutter行业动态与学习参考(2026年8月)
android·flutter
quantdash_cc12 小时前
基于 QuantDash 5 分钟 K 线的网格交易策略参数网格搜索寻优实战
android·开发语言·pandas·量化·quantdash