news 2026/6/10 13:10:15

Launcher进程启动流程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Launcher进程启动流程

1、Launcher

Launcher作为Android系统的桌面,它的作用有两点:
作为Android系统的启动器,用于启动应用程序;
作为Android系统的桌面,用于显示和管理应用程序的快捷图标或者其它桌面组件;

2、Launcher进程启动流程

2.1、SystemServer调用

在SystemServer进程启动之后,执行其run()函数,在里面执行了大量的配置设置操作,并且启动了各种引导服务、核心服务以及其他服务等,包括AMS、PMS、WMS、电量管理服务等一系列服务,以及创建主线程Looper,并循环等待消息;
其中在启动引导服务方法中,启动了ActivityManagerService,并且在启动其他服务的方法中,调用AMS的systemReady()方法,Launcher进程就是从这儿开始启动的;

public final class SystemServer { private void run() { ... startBootstrapServices(); startOtherServices(); ... } private void startBootstrapServices() { ... mActivityManagerService = mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService(); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); ... } private void startOtherServices() { ... mActivityManagerService.systemReady(() -> { }, BOOT_TIMINGS_TRACE_LOG); } }

在SystemServer启动的时候,执行startOtherServices()方法中,里面调用了AMS的systemReady()方法,通过该方法来启动Launcher;

// Tag for timing measurement of main thread. private static final String SYSTEM_SERVER_TIMING_TAG = "SystemServerTiming"; private static final TimingsTraceLog BOOT_TIMINGS_TRACE_LOG = new TimingsTraceLog(SYSTEM_SERVER_TIMING_TAG, Trace.TRACE_TAG_SYSTEM_SERVER); private void startOtherServices() { ... mActivityManagerService.systemReady(() -> { Slog.i(TAG, "Making services ready"); traceBeginAndSlog("StartActivityManagerReadyPhase"); mSystemServiceManager.startBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY); ... }, BOOT_TIMINGS_TRACE_LOG); }

2.2、AMS执行

在AMS中执行systemReady()方法,在其中执行startHomeActivityLocked()方法,传入当前用户ID;

public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) { ... synchronized (this) { ... startHomeActivityLocked(currentUserId, "systemReady"); ... } ... }
2.2.1、获取Launcher的Intent

在startHomeActivityLocked()方法中,首先通过getHomeIntent()方法,获取到要启动的HomeActivity的intent对象,其中mTopAction默认为INTENT.ACTION_MAIN,并添加CATEGORY_HOME的category标志;
得到Intent对象,通过PackageManager去获取对应符合的Activity,获取对应的ActivityInfo,并获取对应的进程记录,此时对应的进程还没启动,后面继续执行,为intent添加FLAG_ACTIVITY_NEW_TASK启动参数,开启新栈,随后调用ActivityStartController类的startHomeActivity()方法去执行启动;

boolean startHomeActivityLocked(int userId, String reason) { ... Intent intent = getHomeIntent(); ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); if (aInfo != null) { 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() | 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; mActivityStartController.startHomeActivity(intent, aInfo, myReason); } } ... return true; } Intent getHomeIntent() { Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null); intent.setComponent(mTopComponent); intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING); if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) { intent.addCategory(Intent.CATEGORY_HOME); } return intent; }
2.2.2、启动Launcher

在startHomeActivity()方法中,调用obtainStarter()方法获取到一个ActivityStarter对象,setCallingUid()方法设置当前调用的Uid=0,然后执行其execute()方法;

void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) { mSupervisor.moveHomeStackTaskToTop(reason); mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason) .setOutActivity(tmpOutRecord) .setCallingUid(0) .setActivityInfo(aInfo) .execute(); mLastHomeActivityStartRecord = tmpOutRecord[0]; 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(); } }

在ActivityStarter的execute()方法中,mayWait默认为false,执行startActivity()方法;

int execute() { try { // TODO(b/64750076): Look into passing request directly to these methods to allow // for transactional diffs and preprocessing. if (mRequest.mayWait) { return startActivityMayWait(mRequest.caller, mRequest.callingUid, ...); } else { return startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent, ...); } } finally { onExecutionComplete(); } }

这里进入了Activity的启动流程,Launcher本身就是一个系统APP,用于显示桌面等,LauncherApp启动之后会执行其生命周期方法初始化桌面布局;

2.3、初始化桌面图标

2.3.1、执行onCreate()方法
@Override protected void onCreate(Bundle savedInstanceState) { ... LauncherAppState app = LauncherAppState.getInstance(this); ... }

获取LauncherAppState,通过LauncherAppState的getInstance()方法获取,该方法里面会判断当前线程是否为主线程,在主线程时还会直接new出对象,不在主线程时,通过MainThreadExecutor的submit()方法向主线程提交一个任务去获取该对象;

// We do not need any synchronization for this variable as its only written on UI thread. private static LauncherAppState INSTANCE; public static LauncherAppState getInstance(final Context context) { if (INSTANCE == null) { if (Looper.myLooper() == Looper.getMainLooper()) { INSTANCE = new LauncherAppState(context.getApplicationContext()); } else { try { return new MainThreadExecutor().submit(new Callable<LauncherAppState>() { @Override public LauncherAppState call() throws Exception { return LauncherAppState.getInstance(context); } }).get(); } catch (InterruptedException|ExecutionException e) { throw new RuntimeException(e); } } } return INSTANCE; }
2.3.2、读取安装APP信息

在LauncherAppState的构造方法中,会新建InvariantDeviceProfile对象,这个类主要是存储App的基本配置信息,例如App图标的尺寸大小,文字大小,每个工作空间或文件夹能显示多少App等;
在LauncherAppState的构造方法中,会获取WindowManager,并获取屏幕的尺寸,解析桌面布局文件,获取默认尺寸信息等;

@TargetApi(23) public InvariantDeviceProfile(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); display.getMetrics(dm); ... ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context)); ... } ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) { ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>(); try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) { final int depth = parser.getDepth(); int type; while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) { TypedArray a = context.obtainStyledAttributes(Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile); int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0); int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0); float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0); profiles.add(new InvariantDeviceProfile( a.getString(R.styleable.InvariantDeviceProfile_name), a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0), a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0), numRows, numColumns, a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows), a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns), iconSize, a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize), a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0), a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns), a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0), a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0))); a.recycle(); } } } catch (IOException|XmlPullParserException e) { throw new RuntimeException(e); } return profiles; }
2.3.3、注册Intent广播

新建LauncherModel对象,该对象是一个BroadcastReceiver,并添加App变化的回调,以及设置Filter并注册广播,用于监听桌面App的变化;

private LauncherAppState(Context context) { ... mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext)); LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel); // Register intent receivers IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_LOCALE_CHANGED); // For handling managed profiles filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); ... mContext.registerReceiver(mModel, filter); ... } public class LauncherModel extends BroadcastReceiver ... {}
2.3.4、解析Launcher布局

继续回到Launcher的onCreate()方法,将Launcher添加到LauncherModel中,是以弱引用的方式添加,初始化一些其工作,解析Launcher的布局,

2.3.5、加载桌面

onCreate()方法中,通过LauncherModel的startLoader()来加载桌面App;

@Override protected void onCreate(Bundle savedInstanceState) { ... if (!mModel.startLoader(currentScreen)) { if (!internalStateHandled) { // If we are not binding synchronously, show a fade in animation when // the first page bind completes. mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0); } } else { // Pages bound synchronously. mWorkspace.setCurrentPage(currentScreen); setWorkspaceLoading(true); } ... }

在LauncherModel的startLoader()方法中,新建了一个LoaderResults对象,并通过startLoaderForResults()方法创建出一个LoaderTask的Runnable任务,将其在工作线程中执行起来;

public boolean startLoader(int synchronousBindPage) { ... synchronized (mLock) { // Don't bother to start the thread if we know it's not going to do anything if (mCallbacks != null && mCallbacks.get() != null) { ... LoaderResults loaderResults = new LoaderResults(mApp, sBgDataModel, mBgAllAppsList, synchronousBindPage, mCallbacks); if (mModelLoaded && !mIsLoaderTaskRunning) { ... return true; } else { startLoaderForResults(loaderResults); } } } return false; } public void startLoaderForResults(LoaderResults results) { synchronized (mLock) { stopLoader(); mLoaderTask = new LoaderTask(mApp, mBgAllAppsList, sBgDataModel, results); runOnWorkerThread(mLoaderTask); } } private static void runOnWorkerThread(Runnable r) { if (sWorkerThread.getThreadId() == Process.myTid()) { r.run(); } else { // If we are not on the worker thread, then post to the worker handler sWorker.post(r); } }

在LoaderTask的run()方法中,去加载手机已安装的App的信息,查询数据库获取已安装的App的相关信息,加载Launcher布局,并将数据转化为View,绑定到界面上,由此我们就可以看到桌面显示的宫格列表的桌面图标了;

public void run() { ... try (LauncherModel.LoaderTransaction transaction = mApp.getModel().beginLoader(this)) { // 查询数据库整理App信息,转化为View绑定到界面 loadWorkspace(); mResults.bindWorkspace(); loadAllApps(); mResults.bindAllApps(); loadDeepShortcuts(); mResults.bindDeepShortcuts(); mBgDataModel.widgetsModel.update(mApp, null); mResults.bindWidgets(); transaction.commit(); } catch (CancellationException e) { // Loader stopped, ignore TraceHelper.partitionSection(TAG, "Cancelled"); } TraceHelper.endSection(TAG); }

AI大模型学习福利

作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获取

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。


因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

四、AI大模型商业化落地方案

因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

作为普通人,入局大模型时代需要持续学习和实践,不断提高自己的技能和认知水平,同时也需要有责任感和伦理意识,为人工智能的健康发展贡献力量

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 7:54:46

【课程设计/毕业设计】基于微信小程序的话剧票务管理系统基于springboot+微信小程序的话剧票务管理系统【附源码、数据库、万字文档】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/6/10 7:59:26

【读书笔记】《日常生活中的自我呈现》

《日常生活中的自我呈现》书籍解读整理 这是一本由加拿大社会学家欧文戈夫曼&#xff08;Erving Goffman&#xff09;撰写的经典著作&#xff0c;将戏剧表演框架引入社会学分析&#xff0c;提出“拟剧论”&#xff08;dramaturgical analysis&#xff09;。戈夫曼认为&#xff…

作者头像 李华
网站建设 2026/6/10 3:33:18

避坑指南:Open Interpreter本地AI编程常见问题全解

避坑指南&#xff1a;Open Interpreter本地AI编程常见问题全解 1. 引言&#xff1a;为什么选择本地化AI编程&#xff1f; 随着大模型技术的普及&#xff0c;开发者对数据隐私、执行效率和系统可控性的要求日益提升。将AI代码生成能力部署在本地&#xff0c;已成为越来越多技术…

作者头像 李华
网站建设 2026/6/10 8:02:20

免费查文献的网站推荐:实用资源汇总与使用指南

做科研的第一道坎&#xff0c;往往不是做实验&#xff0c;也不是写论文&#xff0c;而是——找文献。 很多新手科研小白会陷入一个怪圈&#xff1a;在知网、Google Scholar 上不断换关键词&#xff0c;结果要么信息过载&#xff0c;要么完全抓不到重点。今天分享几个长期使用的…

作者头像 李华
网站建设 2026/6/10 9:26:10

Meta-Llama-3-8B-Instruct一键部署:open-webui可视化界面教程

Meta-Llama-3-8B-Instruct一键部署&#xff1a;open-webui可视化界面教程 1. 引言 随着大语言模型在对话理解、指令遵循和多任务处理能力上的持续进化&#xff0c;本地化部署高性能开源模型已成为开发者和研究者的刚需。Meta于2024年4月发布的Meta-Llama-3-8B-Instruct&#…

作者头像 李华
网站建设 2026/6/10 9:24:27

手机也能跑AI?用DeepSeek-R1-Distill-Qwen-1.5B打造边缘计算助手

手机也能跑AI&#xff1f;用DeepSeek-R1-Distill-Qwen-1.5B打造边缘计算助手 1. 引言&#xff1a;当大模型走向终端设备 近年来&#xff0c;AI大模型的发展速度令人瞩目。从千亿参数的GPT系列到如今轻量级但性能强劲的小模型&#xff0c;边缘AI推理正成为技术演进的重要方向。…

作者头像 李华