Android-Framework学习笔记(四)Launcher启动过程(1),Android高级工程师进阶学习

public void run() {
/**

  • 执行各种SystemService的启动方法,各种SystemService的systemReady方法…
    */
    Slog.i(TAG, “Making services ready”);
    mSystemServiceManager.startBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY);

    }

    }

注释1处调用ActivityManagerService的systemReady函数。

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

ActivityManagerService#systemReady()

public void systemReady(final Runnable goingCallback) {

// Start up initial activity.
mBooting = true;
// Enable home activity for system user, so that the system can always boot
if (UserManager.isSplitSystemUser()) {
ComponentName cName = new ComponentName(mContext, SystemUserHomeActivity.class);
try {
AppGlobals.getPackageManager().setComponentEnabledSetting(cName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0, UserHandle.USER_SYSTEM);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
startHomeActivityLocked(currentUserId, “systemReady”); //1

}

注释1处调用了startHomeActivityLocked方法,看其名字就是说开始执行启动homeActivity的操作。

ActivityManagerService#startHomeActivityLocked()

boolean startHomeActivityLocked(int userId, String reason) {
if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL && mTopAction == null) { //1
// 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 = getHomeIntent(); //2
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.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mActivityStarter.startHomeActivityLocked(intent, aInfo, reason); //3
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}

return true;
}

注释1处的mFactoryTest代表系统的运行模式,系统的运行模式分为三种,分别是非工厂模式、低级工厂模式和高级工厂模式,mTopAction则用来描述第一个被启动Activity组件的Action,它的值为Intent.ACTION_MAIN。因此注释1的代码意思就是mFactoryTest为FactoryTest.FACTORY_TEST_LOW_LEVEL(低级工厂模式)并且mTopAction=null时,直接返回false。
注释2处的getHomeIntent函数如下所示。

ActivityManagerService#getHomeIntent()

Intent getHomeIntent() {
Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null); //1
intent.setComponent(mTopComponent);
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME); //2
}
return intent;
}

注释1中创建了Intent,并将mTopAction和mTopData传入。mTopAction的值为Intent.ACTION_MAIN。
注释2如果系统运行模式不是低级工厂模式则将intent的Category设置为Intent.CATEGORY_HOME。之后被启动的应用程序就是Launcher,因为Launcher的Manifest文件中的intent-filter标签匹配了Action为Intent.ACTION_MAIN,Category为Intent.CATEGORY_HOME。Launcher的Manifest文件如下所示。

packages/apps/Launcher3/AndroidManifest.xml




<application











ActivityManagerService的startHomeActivityLocked()的注释3就是启动符合条件的应用程序,即Launcher。

frameworks/base/services/core/java/com/android/server/am/ActivityStarter.java

ActivityStarter#startHomeActivityLocked()

void startHomeActivityLocked(Intent intent, ActivityInfo aInfo, String reason) {
mSupervisor.moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
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/, null /outActivity/,
null /container/, null /inTask/);
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(); //1
}
}

注释1调用的是scheduleResumeTopActivities()方法,这个方法其实是关于Activity的启动流程的逻辑了,这里我们就不详细说明了,关于Activity的启动流程可以参考我后面文章。

这样Launcher就会被启动起来,并执行它的onCreate函数。

Android应用程序安装
Android系统在启动的过程中,Zygote进程启动SystemServer进程,SystemServer启动PackageManagerService服务,这个服务负责扫描系统中特定的目录,找到里面的应用程序文件,即以Apk为后缀的文件,然后对这些文件进解析(其实就是解析应用程序配置文件AndroidManifest.xml的过程),并从里面得到得到应用程序的相关信息,例如得到应用程序的组件Package、Activity、Service、Broadcast Receiver和Content Provider等信息,保存到PackageManagerService的mPackages、mActivities、mServices、mReceivers等成员变量(HashMap类型)中,得到应用程序的相关信息之后,完成应用程序的安装过程。

这些应用程序只是相当于在PackageManagerService服务注册好了,如果我们想要在Android桌面上看到这些应用程序,还需要有一个Home应用程序(Android系统默认的Home应用程序就是Launcher),负责从PackageManagerService服务中把这些安装好的应用程序取出来,并以友好的方式在桌面上展现出来,例如以快捷图标的形式,接着往下看。

Launcher中应用图标显示流程
从Launcher的onCreate函数开始分析。

packages/apps/Launcher3/src/com/android/launcher3/Launcher.java

Launcher#onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {

LauncherAppState app = LauncherAppState.getInstance();//1
mDeviceProfile = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ?
app.getInvariantDeviceProfile().landscapeProfile
: app.getInvariantDeviceProfile().portraitProfile;
mSharedPrefs = Utilities.getPrefs(this);
mIsSafeModeEnabled = getPackageManager().isSafeMode();
mModel = app.setLauncher(this);//2

if (!mRestoring) {
if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);//3
} else {
mModel.startLoader(mWorkspace.getRestorePage());
}
}

}

注释1处获取LauncherAppState的实例。
注释2处调用它的setLauncher函数并将Launcher对象传入。

packages/apps/Launcher3/src/com/android/launcher3/LauncherAppState.java

LauncherAppState#setLauncher()

LauncherModel setLauncher(Launcher launcher) {
getLauncherProvider().setLauncherProviderChangeListener(launcher);
mModel.initialize(launcher);//1
mAccessibilityDelegate = ((launcher != null) && Utilities.ATLEAST_LOLLIPOP) ? new LauncherAccessibilityDelegate(launcher) : null;
return mModel;
}

注释1处会调用LauncherModel的initialize函数。

packages/apps/Launcher3/src/com/android/launcher3/LauncherModel.java

LauncherModel#initialize()

public void initialize(Callbacks callbacks) {
synchronized (mLock) {
unbindItemInfosAndClearQueuedBindRunnables();
mCallbacks = new WeakReference(callbacks);
}
}

在initialize函数中会将Callbacks,也就是传入的Launcher封装成一个弱引用对象。因此我们得知mCallbacks变量指的就是封装成弱引用对象的Launcher,这个mCallbacks后文会用到它。

再回到Launcher的onCreate函数,在注释3处调用了LauncherModel的startLoader函数:

LauncherModel#startLoader()


@Thunk static final HandlerThread sWorkerThread = new HandlerThread(“launcher-loader”);//1
static {
sWorkerThread.start();
}
@Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());//2

public void startLoader(int synchronousBindPage, int loadFlags) {
InstallShortcutReceiver.enableInstallQueue();
synchronized (mLock) {
synchronized (mDeferredBindRunnables) {
mDeferredBindRunnables.clear();
}
if (mCallbacks != null && mCallbacks.get() != null) {
stopLoaderLocked();
mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags); //3
if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

考虑到文章的篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档

喜欢的朋友可以关注、转发、点赞 感谢!

且后续会持续更新**

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-zx6qzWkW-1711731951429)]

最后

考虑到文章的篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档

[外链图片转存中…(img-OVYbEPjz-1711731951429)]

[外链图片转存中…(img-zXIku2Em-1711731951430)]

喜欢的朋友可以关注、转发、点赞 感谢!

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值