启动流程为什么要相识
不相识 ,平常开发好像也没啥标题啊。实在否则,办理这些标题后,你会对Android体系有更深条理的明白,也会学习到体系源码精良的计划。而且对办理一些高级标题和深入的性能优化标题有很大资助,是技能进阶的须要阶段。这就必要我们通过阅读源码来梳理这些标题,但另一方面,体系源码是很巨大繁杂的,我们必要带着标题捉住主流程,不能陷入代码细节——这是阅读体系源码以及其他第三方库源码的正确姿势。
1, 体系开机到启动Launcher应用桌面的流程
手机开时机启动体系boolt,会创建zogote进程-->JVM-->启动systm_server-->会启动体系AMS 、WMS、PMS、广播、WIFI、电量等服务//体系源码 public final class SystemServer { /** * The main entry point from zygote. */ public static void main(String[] args) { new SystemServer().run(); } private void run() { // Start services. //这里启动了几个重要的服务 startBootstrapServices(); startCoreServices(); startOtherServices(); }//这里启动了 AMS、PMS private void startBootstrapServices() { //启动了SystemServiceManager traceBeginAndSlog("StartInstaller"); Installer installer = mSystemServiceManager.startService(Installer.class); traceEnd(); //启动了StartActivityManager AMS // Activity manager runs the show. traceBeginAndSlog("StartActivityManager"); mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.LifIApplicationThreadIApplicationThreadecycle.class).getService(); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); traceEnd(); //启动了PackageManagerService PMS // Start the package manager. traceBeginAndSlog("StartPackageManagerService"); mPackageManagerService = PackageManagerService.main(mSystemContext, installer, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); mFirstBoot = mPackageManagerService.isFirstBoot(); mPackageManager = mSystemContext.getPackageManager(); traceEnd(); traceBeginAndSlog("StartUserManagerService"); mSystemServiceManager.startService(UserManagerService.LifeCycle.class); traceEnd(); }2,Launcher点击应用桌面app启动流程,根Activity的启动—应用进程启动
我们知道,想要启动一个应用步伐(App),必要点击手机桌面的应用图标。Android体系的桌面叫做Launcher,有以下作用:
作为Android体系的启动器,用于启动应用步伐。
作为Android体系的桌面,用于表现和管理应用步伐的快捷图标和其他桌面组件。
Launcher本身也是一个应用步伐,它在启动过程中会哀求PackageManageService(体系的包管理服务)返回体系中已经安装的app的信息,并将其用快捷图标展示在桌面屏幕上,用户可以点击图标启动app。比方华为手机的Launcher就是 “华为桌面” 这个体系app。
当点击app图标后,Launcher会在桌面activity(此activity就叫Launcher)内调用startActivitySafely方法,startActivitySafely方法会调用startActivity方法。
应用桌面是一个LauncherActicity页面,内里有点击事故
分析源码
Intent intent = new Intent(this, TestActivity.class); this.startActivity(intent);//下面我们就来对Activity的工作流程举行梳理,到达对Activity团体流程的把握。从startActivity方法开始,会走到
@Override public void startActivity(Intent intent, @Nullable Bundle options) { if (mIntent != null && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN) && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY)) { if (TextUtils.equals(getPackageName(), intent.resolveActivity(getPackageManager()).getPackageName())) { // Apply Autofill restore mechanism on the started activity by startActivity() final IBinder token = mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN); // Remove restore ability from current activity mIntent.removeExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN); mIntent.removeExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY); // Put restore token intent.putExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN, token); intent.putExtra(AutofillManager.EXTRA_RESTORE_CROSS_ACTIVITY, true); } } if (options != null) { startActivityForResult(intent, -1, options); } else { // Note we want to go through this call for compatibility with // applications that may have overridden the method. startActivityForResult(intent, -1); } }startActivityForResult方法:
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { if (mParent == null) { options = transferSpringboardActivityOptions(options); Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, options); if (ar != null) { mMainThread.sendActivityResult( mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); } if (requestCode >= 0) { mStartedActivity = true; } cancelInputsAndStartExitTransition(options); } else { ... } }看到内里调用了mInstrumentation.execStartActivity方法,此中一个参数mMainThread.getApplicationThread(),它的类型是ApplicationThread,ApplicationThread是ActivityThread的内部类,继承IApplicationThread.Stub,也是个Binder对象,在Activity工作流程中有重要作用。而Instrumentation具有跟踪application及activity生命周期的功能,用于android 应用测试框架中代码检测。接着看下mInstrumentation.execStartActivity方法:
public ActivityResult execStartActivity( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) { IApplicationThread whoThread = (IApplicationThread) contextThread; Uri referrer = target != null ? target.onProvideReferrer() : null; if (referrer != null) { intent.putExtra(Intent.EXTRA_REFERRER, referrer); } ... try { intent.migrateExtraStreamToClipData(); intent.prepareToLeaveProcess(who); int result = ActivityTaskManager.getService() .startActivity(whoThread, who.getBasePackageName(), intent, intent.resolveTypeIfNeeded(who.getContentResolver()), token, target != null ? target.mEmbeddedID : null, requestCode, 0, null, options); checkStartActivityResult(result, intent); } catch (RemoteException e) { throw new RuntimeException("Failure from system", e); } return null; }这里看到Activity的启动又交给了ActivityTaskManager.getService(),这是啥?跟进去看看:
//ActivityTaskManager public static IActivityTaskManager getService() { return IActivityTaskManagerSingleton.get(); } private static final Singleton<IActivityTaskManager> IActivityTaskManagerSingleton = new Singleton<IActivityTaskManager>() { @Override protected IActivityTaskManager create() { final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE); return IActivityTaskManager.Stub.asInterface(b); } };看到IBinder这个标记,这里你应该明白了:这里是获取一个跨进程的服务。获取的什么服务呢?是ActivityTaskManagerService(ATMS),它继承于IActivityTaskManager.Stub,是个Binder对象,而且是通过单例提供服务的。 ATMS是用于管理Activity及其容器(使命、堆栈、表现等)的体系服务,运行在体系服务进程(system_server)之中。
值得阐明的是,ATMS是在Android10中新增的,分担了之前ActivityManagerService(AMS)的一部分功能(activity task相干)。
在Android10 之前 ,这个地方获取的是服务是AMS。检察Android10的AMS,你会发现startActivity方法内也是调用了ATMS的startActivity方法。以是在明白上,ATMS就从属于AMS。
接着看,ActivityTaskManager.getService().startActivity有个返回值result,且调用了checkStartActivityResult(result, intent): public static void checkStartActivityResult(int res, Object intent) { if (!ActivityManager.isStartResultFatalError(res)) { return; } switch (res) { case ActivityManager.START_INTENT_NOT_RESOLVED: case ActivityManager.START_CLASS_NOT_FOUND: if (intent instanceof Intent && ((Intent)intent).getComponent() != null) throw new ActivityNotFoundException( "Unable to find explicit activity class " + ((Intent)intent).getComponent().toShortString() + "; have you declared this activity in your AndroidManifest.xml?"); throw new ActivityNotFoundException( "No Activity found to handle " + intent); case ActivityManager.START_PERMISSION_DENIED: throw new SecurityException("Not allowed to start activity " + intent); ... case ActivityManager.START_CANCELED: throw new AndroidRuntimeException("Activity could not be started for " + intent); default: throw new AndroidRuntimeException("Unknown error code " + res + " when starting " + intent); } }Activity的管理——AMS
好了,到这里,Activity的启动就跨进程(IPC)的转移到体系进程提供的服务ATMS中了,接着看ATMS的startActivity |