Android进阶之App启动流程和源码详解

时间:2022-09-12 09:52:27

Android进阶之App启动流程和源码详解

前言

Android中有四大组件:Activity、Service、BroadcastReceiver、ContentProvider。我们最常接触也是用户直接感受到的就是Activity了,今天来就说说Android启动的执行过程和工作原理。

Activity是一种 展示型组件,用于展示给用户一个可以交互的页面。Activity是Android中最重要的组件,对用户来说,所有的Activity就是一个App的全部,而其他组件用户是不能直接感知的;

今天我们就来分析总结下app启动流程的场景和知识点

一、启动中设计到关键类详解

1、启动流程设计到主要类

  • ActivityManagerServices,简称AMS,服务端对象,负责系统中所有Activity的生命周期;
  • ActivityThread,App的真正入口。当开启App之后,会调用main()开始运行,开启消息循环队列,这就是传说中的UI线程或者叫主线程。与ActivityManagerServices配合,一起完成Activity的管理工作;
  • ApplicationThread,用来实现ActivityManagerService与ActivityThread之间的交互。在ActivityManagerService需要管理相关Application中的Activity的生命周期时,通过ApplicationThread的代理对象与ActivityThread通讯;
  • ApplicationThreadProxy,是ApplicationThread在服务器端的代理,负责和客户端的ApplicationThread通讯。AMS就是通过该代理与ActivityThread进行通信的;
  • Instrumentation,每一个应用程序只有一个Instrumentation对象,每个Activity内都有一个对该对象的引用。Instrumentation可以理解为应用进程的管家,ActivityThread要创建或暂停某个Activity时,都需要通过Instrumentation来进行具体的操作;
  • ActivityStack,Activity在AMS的栈管理,用来记录已经启动的Activity的先后关系,状态信息等。通过ActivityStack决定是否需要启动新的进程;
  • ActivityRecord,ActivityStack的管理对象,每个Activity在AMS对应一个ActivityRecord,来记录Activity的状态以及其他的管理信息。其实就是服务器端的Activity对象的映像;
  • TaskRecord,AMS抽象出来的一个“任务”的概念,是记录ActivityRecord的栈,一个“Task”包含若干个ActivityRecord。AMS用TaskRecord确保Activity启动和退出的顺序。如果你清楚Activity的4种launchMode,那么对这个概念应该不陌生;
  • 可以说,启动的流程就是通过这几个类之间不断通信的过程;

2、zygote

  • zygote意为“受精卵“。Android是基于Linux系统的,而在Linux中,所有的进程都是由init进程直接或者是间接fork出来的,zygote进程也不例外。
  • 在Android系统里面,zygote是一个进程的名字。Android是基于Linux System的,当你的手机开机的时候,Linux的内核加载完成之后就会启动一个叫“init“的进程。在Linux System里面,所有的进程都是由init进程fork出来的,我们的zygote进程也不例外。
  • 我们都知道,每一个App其实都是:一个单独的dalvik虚拟机
  • 一个单独的进程;
  • 所以当系统里面的第一个zygote进程运行之后,在这之后再开启App,就相当于开启一个新的进程。而为了实现资源共用和更快的启动速度,Android系统开启新进程的方式,是通过fork第一个zygote进程实现的。所以说,除了第一个zygote进程,其他应用所在的进程都是zygote的子进程;

3、SystemServer

SystemServer也是一个进程,而且是由zygote进程fork出来的

在zygote开启的时候,会调用ZygoteInit.main()进行初始化

  1. public static void main(String argv[]) { 
  2.      ...ignore some code... 
  3.     //在加载首个zygote的时候,会传入初始化参数,使得startSystemServer = true 
  4.      boolean startSystemServer = false
  5.      for (int i = 1; i < argv.length; i++) { 
  6.                 if ("start-system-server".equals(argv[i])) { 
  7.                     startSystemServer = true
  8.                 } else if (argv[i].startsWith(ABI_LIST_ARG)) { 
  9.                     abiList = argv[i].substring(ABI_LIST_ARG.length()); 
  10.                 } else if (argv[i].startsWith(SOCKET_NAME_ARG)) { 
  11.                     socketName = argv[i].substring(SOCKET_NAME_ARG.length()); 
  12.                 } else { 
  13.                     throw new RuntimeException("Unknown command line argument: " + argv[i]); 
  14.                 } 
  15.             } 
  16.             ...ignore some code... 
  17.          //开始fork我们的SystemServer进程 
  18.      if (startSystemServer) { 
  19.                 startSystemServer(abiList, socketName); 
  20.          } 
  21.      ...ignore some code... 

startSystemServer()做了些什么

  1. /**留着这个注释,就是为了说明SystemServer确实是被fork出来的 
  2.      * Prepare the arguments and fork for the system server process. 
  3.      */ 
  4.     private static boolean startSystemServer(String abiList, String socketName) 
  5.             throws MethodAndArgsCaller, RuntimeException { 
  6.          ...ignore some code... 
  7.         //留着这段注释,就是为了说明上面ZygoteInit.main(String argv[])里面的argv就是通过这种方式传递进来的 
  8.         /* Hardcoded command line to start the system server */ 
  9.         String args[] = { 
  10.             "--setuid=1000"
  11.             "--setgid=1000"
  12.             "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1032,3001,3002,3003,3006,3007"
  13.             "--capabilities=" + capabilities + "," + capabilities, 
  14.             "--runtime-init"
  15.             "--nice-name=system_server"
  16.             "com.android.server.SystemServer"
  17.         }; 
  18.         int pid; 
  19.         try { 
  20.             parsedArgs = new ZygoteConnection.Arguments(args); 
  21.             ZygoteConnection.applyDebuggerSystemProperty(parsedArgs); 
  22.             ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs); 
  23.         //fork出来的 -_-||| 
  24.             /* Request to fork the system server process */ 
  25.             pid = Zygote.forkSystemServer( 
  26.                     parsedArgs.uid, parsedArgs.gid, 
  27.                     parsedArgs.gids, 
  28.                     parsedArgs.debugFlags, 
  29.                     null
  30.                     parsedArgs.permittedCapabilities, 
  31.                     parsedArgs.effectiveCapabilities); 
  32.         } catch (IllegalArgumentException ex) { 
  33.             throw new RuntimeException(ex); 
  34.         } 
  35.         /* For child process */ 
  36.         if (pid == 0) { 
  37.             if (hasSecondZygote(abiList)) { 
  38.                 waitForSecondaryZygote(socketName); 
  39.             } 
  40.             handleSystemServerProcess(parsedArgs); 
  41.         } 
  42.         return true
  43.     } 

4、ActivityManagerService

  • ActivityManagerService,简称AMS,服务端对象,负责系统中所有Activity的生命周期。
  • ActivityManagerService进行初始化的时机很明确,就是在SystemServer进程开启的时候,就会初始化ActivityManagerService。
  1. public final class SystemServer { 
  2.     //zygote的主入口 
  3.     public static void main(String[] args) { 
  4.         new SystemServer().run(); 
  5.     } 
  6.     public SystemServer() { 
  7.         // Check for factory test mode. 
  8.         mFactoryTestMode = FactoryTest.getMode(); 
  9.     } 
  10.     private void run() { 
  11.         ...ignore some code... 
  12.         //加载本地系统服务库,并进行初始化  
  13.         System.loadLibrary("android_servers"); 
  14.         nativeInit(); 
  15.         // 创建系统上下文 
  16.         createSystemContext(); 
  17.         //初始化SystemServiceManager对象,下面的系统服务开启都需要调用SystemServiceManager.startService(Class<T>),这个方法通过反射来启动对应的服务 
  18.         mSystemServiceManager = new SystemServiceManager(mSystemContext); 
  19.         //开启服务 
  20.         try { 
  21.             startBootstrapServices(); 
  22.             startCoreServices(); 
  23.             startOtherServices(); 
  24.         } catch (Throwable ex) { 
  25.             Slog.e("System""******************************************"); 
  26.             Slog.e("System""************ Failure starting system services", ex); 
  27.             throw ex; 
  28.         } 
  29.         ...ignore some code... 
  30.     } 

初始化系统上下文对象mSystemContext,并设置默认的主题,mSystemContext实际上是一个ContextImpl对象。调用ActivityThread.systemMain()的时候,会调用ActivityThread.attach(true),而在attach()里面,则创建了Application对象,并调用了Application.onCreate()。

  1. private void createSystemContext() { 
  2.         ActivityThread activityThread = ActivityThread.systemMain(); 
  3.         mSystemContext = activityThread.getSystemContext(); 
  4.         mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar); 
  5.     } 
  6.     //在这里开启了几个核心的服务,因为这些服务之间相互依赖,所以都放在了这个方法里面。 
  7.     private void startBootstrapServices() { 
  8.         ...ignore some code... 
  9.         //初始化ActivityManagerService 
  10.         mActivityManagerService = mSystemServiceManager.startService( 
  11.                 ActivityManagerService.Lifecycle.class).getService(); 
  12.         mActivityManagerService.setSystemServiceManager(mSystemServiceManager); 
  13.         //初始化PowerManagerService,因为其他服务需要依赖这个Service,因此需要尽快的初始化 
  14.         mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class); 
  15.         // 现在电源管理已经开启,ActivityManagerService负责电源管理功能 
  16.         mActivityManagerService.initPowerManagement(); 
  17.         // 初始化DisplayManagerService 
  18.         mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class); 
  19.     //初始化PackageManagerService 
  20.     mPackageManagerService = PackageManagerService.main(mSystemContext, mInstaller, 
  21.        mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); 
  22.     ...ignore some code... 
  23.     } 

二、app启动流程图步骤介绍

Android进阶之App启动流程和源码详解

1、activity中的startActivity方法最终都会通过拿到ATSM的代理IActivityTaskManager调用的startActivity;

2、之后进入system server进程中的ATMS startActivity,ATMS 经过收集Intent信息,然后使用ActivityStackSupervisor.startSpecificActivityLocked,如果进程已经存在,则直接使用realStartActivityLocked,通过App的binder客户端的代理ApplicationThread调用回到bindApplication,走入Activity的启动流程;如果进程不存在则通过socket链接Zygote,请求fork新的进程;

3、App进程创建完成后,进程启动会调用ActivityThread.main方法,初始化主线程Handler,接着走入attach方法,然后通过AMS的代理调用AMS的attachApplication方法,并将App进程的通信代理ApplicationThread传入AMS;

4、AMS获取到ATMS调用ApplicationThread的bindApplication回到App进程的ActivityThread.ApplicationThread.bindApplication方法中,然后使用Handler切换到主线程执行handleBindApplication,这里初始化了App的进程名字、时间,用户的硬件配置,包括App的文件系统,创建了App的Context实例,Instrumentation实例,调用App的onCreate回调方法,同时告诉AMS APP初始化工作完毕;

5、AMS接着会调用ATMS的attachApplication,最后调用ClientLifecycleManager的scheduleTransaction方法,通过App的Binder代理ApplicationThread回到ActivityThread;

6、进入ActivityThread.ApplicationThread.scheduleTransaction方法之后就进入了Activity的onStart、onResume回调

创建进程之前的过程主要是AMS的内部信息收集的判断的过程,下面主要看一下App进程启动的源码流程;

三、启动源码流程解析

1、APP进程起点

一个App的程序入口到底是什么?是ActivityThread.main()

从应用进程被创建开始,ActivityThread.main被执行

  1. public static void main(String[] args) { 
  2.     ActivityThread thread = new ActivityThread(); 
  3.     thread.attach(false, startSeq); 
  4.     if (sMainThreadHandler == null) { 
  5.         sMainThreadHandler = thread.getHandler(); 
  6.     } 
  7.     if (false) { 
  8.         Looper.myLooper().setMessageLogging(new 
  9.                 LogPrinter(Log.DEBUG, "ActivityThread")); 
  10.     } 
  11.     // End of event ActivityThreadMain. 
  12.     Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); 
  13.     Looper.loop(); 
  14.     throw new RuntimeException("Main thread loop unexpectedly exited"); 

调用ActivityThread的attach方法,然后将activity和AMS通信的Binder代理IApplicationThread实例传入AMS

  1. @UnsupportedAppUsage 
  2. private void attach(boolean system, long startSeq) { 
  3.     sCurrentActivityThread = this; 
  4.     mSystemThread = system; 
  5.     if (!system) { 
  6.         android.ddm.DdmHandleAppName.setAppName("<pre-initialized>"
  7.                                                 UserHandle.myUserId()); 
  8.         RuntimeInit.setApplicationObject(mAppThread.asBinder()); 
  9.         final IActivityManager mgr = ActivityManager.getService(); 
  10.         try { 
  11.             mgr.attachApplication(mAppThread, startSeq); 
  12.         } catch (RemoteException ex) { 
  13.             throw ex.rethrowFromSystemServer(); 
  14.         } 
  15.         // Watch for getting close to heap limit. 
  16.         BinderInternal.addGcWatcher(new Runnable() { 
  17.             @Override public void run() { 
  18.                 if (!mSomeActivitiesChanged) { 
  19.                     return
  20.                 } 
  21.                 Runtime runtime = Runtime.getRuntime(); 
  22.                 long dalvikMax = runtime.maxMemory(); 
  23.                 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory(); 
  24.                 if (dalvikUsed > ((3*dalvikMax)/4)) { 
  25.                     if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024) 
  26.                             + " total=" + (runtime.totalMemory()/1024) 
  27.                             + " used=" + (dalvikUsed/1024)); 
  28.                     mSomeActivitiesChanged = false
  29.                     try { 
  30.                         ActivityTaskManager.getService().releaseSomeActivities(mAppThread); 
  31.                     } catch (RemoteException e) { 
  32.                         throw e.rethrowFromSystemServer(); 
  33.                     } 
  34.                 } 
  35.             } 
  36.         }); 
  37.     }  

2、AMS的attachApplication

接着进入AMS进程,ActivityManagerService.attachApplicationLocked

  1. private final boolean attachApplicationLocked(IApplicationThread thread, 
  2.             int pid, int callingUid, long startSeq) { 
  3.             if (app.isolatedEntryPoint != null) { 
  4.                 // This is an isolated process which should just call an entry point instead of 
  5.                 // being bound to an application. 
  6.                 thread.runIsolatedEntryPoint(app.isolatedEntryPoint, app.isolatedEntryPointArgs); 
  7.             } else if (instr2 != null) { 
  8.                 thread.bindApplication(processName, appInfo, providers, 
  9.                         instr2.mClass, 
  10.                         profilerInfo, instr2.mArguments, 
  11.                         instr2.mWatcher, 
  12.                         instr2.mUiAutomationConnection, testMode, 
  13.                         mBinderTransactionTrackingEnabled, enableTrackAllocation, 
  14.                         isRestrictedBackupMode || !normalMode, app.isPersistent(), 
  15.                         new Configuration(app.getWindowProcessController().getConfiguration()), 
  16.                         app.compat, getCommonServicesLocked(app.isolated), 
  17.                         mCoreSettingsObserver.getCoreSettingsLocked(), 
  18.                         buildSerial, autofillOptions, contentCaptureOptions); 
  19.             } else { 
  20.                 thread.bindApplication(processName, appInfo, providers, null, profilerInfo, 
  21.                         nullnullnull, testMode, 
  22.                         mBinderTransactionTrackingEnabled, enableTrackAllocation, 
  23.                         isRestrictedBackupMode || !normalMode, app.isPersistent(), 
  24.                         new Configuration(app.getWindowProcessController().getConfiguration()), 
  25.                         app.compat, getCommonServicesLocked(app.isolated), 
  26.                         mCoreSettingsObserver.getCoreSettingsLocked(), 
  27.                         buildSerial, autofillOptions, contentCaptureOptions); 
  28.             } 
  29.         } catch (Exception e) { 
  30.         } 
  31.         // See if the top visible activity is waiting to run in this process... 
  32.         if (normalMode) { 
  33.             try { 
  34.                 didSomething = mAtmInternal.attachApplication(app.getWindowProcessController()); 
  35.             } catch (Exception e) { 
  36.                 Slog.wtf(TAG, "Exception thrown launching activities in " + app, e); 
  37.                 badApp = true
  38.             } 
  39.         } 
  40.         // Find any services that should be running in this process... 
  41.         if (!badApp) { 
  42.             try { 
  43.                 didSomething |= mServices.attachApplicationLocked(app, processName); 
  44.                 checkTime(startTime, "attachApplicationLocked: after mServices.attachApplicationLocked"); 
  45.             } catch (Exception e) { 
  46.                 Slog.wtf(TAG, "Exception thrown starting services in " + app, e); 
  47.                 badApp = true
  48.             } 
  49.         } 
  50.         return true
  51.     } 
  • thread.bindApplication:该方法主要讲App进程的配置信息通过IApplicationThread Binder通信回传到ActivityThread中;
  • mAtmInternal.attachApplication:mAtmInternal实际就是ActivityTaskManager的实例,通过LocalServices加载;
  1. mAtmInternal = LocalServices.getService(ActivityTaskManagerInternal.class); 

那么这里相当于走到了ActivityTaskManagerServer的attachApplication中;

3、APP的bindApplication

先看第一条:

注意:ActivityThread中存在于Binder通信的代理--》ApplicationThread extends IApplicationThread.Stub

ActivityThread--》ApplicationThread--》bindApplication

  1. @Override 
  2.         public final void bindApplication(String processName, ApplicationInfo appInfo, 
  3.                 ProviderInfoList providerList, ComponentName instrumentationName, 
  4.                 ProfilerInfo profilerInfo, Bundle instrumentationArgs, 
  5.                 IInstrumentationWatcher instrumentationWatcher, 
  6.                 IUiAutomationConnection instrumentationUiConnection, int debugMode, 
  7.                 boolean enableBinderTracking, boolean trackAllocation, 
  8.                 boolean isRestrictedBackupMode, boolean persistent, Configuration config, 
  9.                 CompatibilityInfo compatInfo, Map services, Bundle coreSettings, 
  10.                 String buildSerial, AutofillOptions autofillOptions, 
  11.                 ContentCaptureOptions contentCaptureOptions, long[] disabledCompatChanges) { 
  12.             setCoreSettings(coreSettings); 
  13.             AppBindData data = new AppBindData(); 
  14.             data.processName = processName; 
  15.             ..... 
  16.             sendMessage(H.BIND_APPLICATION, data); 
  17.         } 

这里的bindApplication主要初始化了AppBindData,然后发送BIND_APPLICATION给APP的主线程BIND_APPLICATION,最后执行了handleBindApplication

handleBindApplication如下:

  1. ActivityThread--》class H extends Handler 
  2.  case BIND_APPLICATION: 
  3.                     Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication"); 
  4.                     AppBindData data = (AppBindData)msg.obj; 
  5.                     handleBindApplication(data); 
  6.                     Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); 
  7.                     break; 
  8. Application的onCreate方法 
  9. private void handleBindApplication(AppBindData data) { 
  10.           mBoundApplication = data; 
  11.         mConfiguration = new Configuration(data.config); 
  12.         mCompatConfiguration = new Configuration(data.config); 
  13.          if (data.initProfilerInfo != null) { 
  14.             mProfiler.profileFile = data.initProfilerInfo.profileFile; 
  15.             mProfiler.profileFd = data.initProfilerInfo.profileFd; 
  16.             mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval; 
  17.             mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler; 
  18.             mProfiler.streamingOutput = data.initProfilerInfo.streamingOutput; 
  19.             if (data.initProfilerInfo.attachAgentDuringBind) { 
  20.                 agent = data.initProfilerInfo.agent; 
  21.             } 
  22.         } 
  23. mInstrumentationPackageName = ii.packageName; 
  24.             mInstrumentationAppDir = ii.sourceDir; 
  25.             mInstrumentationSplitAppDirs = ii.splitSourceDirs; 
  26.             mInstrumentationLibDir = getInstrumentationLibrary(data.appInfo, ii); 
  27.             mInstrumentedAppDir = data.info.getAppDir(); 
  28.             mInstrumentedSplitAppDirs = data.info.getSplitAppDirs(); 
  29.             mInstrumentedLibDir = data.info.getLibDir(); 
  30.              mInstrumentation.callApplicationOnCreate(app); 

该方法主要在App进程中对App的一些硬件资源配置申请的属性、App的文件夹等完成App基本信息的初始化

4、ATMS的attachApplication

接着看第二条:mAtmInternal.attachApplication

  1. mAtmInternal.attachApplication最终会调用mRootActivityContainer.attachApplication(wpc) 
  2. @HotPath(caller = HotPath.PROCESS_CHANGE) 
  3.         @Override 
  4.         public boolean attachApplication(WindowProcessController wpc) throws RemoteException { 
  5.             synchronized (mGlobalLockWithoutBoost) { 
  6.                 return mRootActivityContainer.attachApplication(wpc); 
  7.             } 
  8.         } 
  9. RootActivityContainer.attachApplication 
  10. boolean attachApplication(WindowProcessController app) throws RemoteException { 
  11.     final String processName = app.mName; 
  12.     boolean didSomething = false
  13.     for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) { 
  14.         final ActivityDisplay display = mActivityDisplays.get(displayNdx); 
  15.         final ActivityStack stack = display.getFocusedStack(); 
  16.         if (stack != null) { 
  17.             stack.getAllRunningVisibleActivitiesLocked(mTmpActivityList); 
  18.             //获取前台栈顶第一个非finishing的Activity 
  19.             final ActivityRecord top = stack.topRunningActivityLocked(); 
  20.             final int size = mTmpActivityList.size(); 
  21.             for (int i = 0; i < size; i++) { 
  22.                 final ActivityRecord activity = mTmpActivityList.get(i); 
  23.                 if (activity.app == null && app.mUid == activity.info.applicationInfo.uid 
  24.                         && processName.equals(activity.processName)) { 
  25.                     try { 
  26.                     //真正启动Activity 
  27.                         if (mStackSupervisor.realStartActivityLocked(activity, app, 
  28.                                 top == activity /* andResume */, true /* checkConfig */)) { 
  29.                             didSomething = true
  30.                         } 
  31.                     } catch (RemoteException e) { 
  32.                         Slog.w(TAG, "Exception in new application when starting activity " 
  33.                                 + top.intent.getComponent().flattenToShortString(), e); 
  34.                         throw e; 
  35.                     } 
  36.                 } 
  37.             } 
  38.         } 
  39.     } 
  40.     if (!didSomething) { 
  41.         ensureActivitiesVisible(null, 0, false /* preserve_windows */); 
  42.     } 
  43.     return didSomething; 

接着调用ActivityStackSupervisor.realStartActivityLocked开始创建Activity

5、ASS的realStartActivityLocked

  1. ActivityStackSupervisor.realStartActivityLocked 
  2. final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app, 
  3.             boolean andResume, boolean checkConfig) throws RemoteException { 
  4.         try { 
  5.             r.startFreezingScreenLocked(app, 0); 
  6.             //启动tick,收集应用启动慢的信息 
  7.             // schedule launch ticks to collect information about slow apps. 
  8.             r.startLaunchTickingLocked(); 
  9.             r.setProcess(app); 
  10.             try { 
  11.                 //创建Activity启动事务 
  12.                 // Create activity launch transaction
  13.                 final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread, 
  14.                         r.appToken); 
  15.                 clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent), 
  16.                         System.identityHashCode(r), r.info, 
  17.                         // TODO: Have this take the merged configuration instead of separate global 
  18.                         // and override configs. 
  19.                         mergedConfiguration.getGlobalConfiguration(), 
  20.                         mergedConfiguration.getOverrideConfiguration(), r.compat, 
  21.                         r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle, 
  22.                         r.persistentState, results, newIntents, mService.isNextTransitionForward(), 
  23.                         profilerInfo)); 
  24.                 //设置目标事务的状态为onResume 
  25.                 // Set desired final state. 
  26.                 final ActivityLifecycleItem lifecycleItem; 
  27.                 if (andResume) { 
  28.                     lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward()); 
  29.                 } else { 
  30.                     lifecycleItem = PauseActivityItem.obtain(); 
  31.                 } 
  32.                 clientTransaction.setLifecycleStateRequest(lifecycleItem); 
  33.                 //通过transaciton方式开始activity生命周期,onCreate,onStart,onResume 
  34.                 // Schedule transaction
  35.                 mService.getLifecycleManager().scheduleTransaction(clientTransaction); 
  36.             } catch (RemoteException e) { 
  37.             } 
  38.         } finally { 
  39.             endDeferResume(); 
  40.         } 
  41.         return true
  42.     } 
  43. ClientLifecycleManager 

创建ClientLifecycleManager和ClientTransactionHandler来辅助管理Activity的生命周期,注意

clientTransaction.addCallback是LaunchActivityItem

lifecycleItem是ResumeActivityItem

ClientLifecycleManager.scheduleTransaction最终会调用ClientTransaction的schedule方法

  1. public void schedule() throws RemoteException { 
  2.     mClient.scheduleTransaction(this); 

那么这个mClient是IApplicationThread的实例,那么此时也就回到了ActivityThread的ApplicationThread中

  1. IApplicationThread getClient() { 
  2.     return mClient; 

6、Activity的onCreate方法

APP的scheduleTransaction

  1. ActivityThread的ApplicationThread中 
  2.  @Override 
  3.   public void scheduleTransaction(ClientTransaction transaction) throws RemoteException { 
  4.         ActivityThread.this.scheduleTransaction(transaction); 
  5.   } 

因为ActivityThread继承ClientTransactionHandler,所以到了ClientTransactionHandler中

  1. void scheduleTransaction(ClientTransaction transaction) { 
  2.       transaction.preExecute(this); 
  3.       sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction); 
  4.  } 

通过Handler发送消息EXECUTE_TRANSACTION到H中

  1. case EXECUTE_TRANSACTION: 
  2.                     final ClientTransaction transaction = (ClientTransaction) msg.obj; 
  3.                     mTransactionExecutor.execute(transaction); 
  4.                     if (isSystem()) { 
  5.                         // Client transactions inside system process are recycled on the client side 
  6.                         // instead of ClientLifecycleManager to avoid being cleared before this 
  7.                         // message is handled. 
  8.                         transaction.recycle(); 
  9.                     } 
  10.                     // TODO(lifecycler): Recycle locally scheduled transactions. 
  11.                     break; 

接着TransactionExecutor的execute方法

  1. public void execute(ClientTransaction transaction) { 
  2.         final IBinder token = transaction.getActivityToken(); 
  3.         log("Start resolving transaction for client: " + mTransactionHandler + ", token: " + token); 
  4.         executeCallbacks(transaction); 
  5.         executeLifecycleState(transaction); 
  6.         mPendingActions.clear(); 
  7.         log("End resolving transaction"); 
  8.     } 
  9. LaunchActivityItem.execute方法 
  10.  @Override 
  11.     public void execute(ClientTransactionHandler client, IBinder token, 
  12.             PendingTransactionActions pendingActions) { 
  13.         Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); 
  14.         ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo, 
  15.                 mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState, 
  16.                 mPendingResults, mPendingNewIntents, mIsForward, 
  17.                 mProfilerInfo, client); 
  18.         client.handleLaunchActivity(r, pendingActions, null /* customIntent */); 
  19.         Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); 
  20.     } 
  21. client其实是在ActivityThread的实例,那么就回到了ActivityThread的handleLaunchActivity 
  22.  @Override 
  23.     public Activity handleLaunchActivity(ActivityClientRecord r, 
  24.             PendingTransactionActions pendingActions, Intent customIntent) { 
  25.             final Activity a = performLaunchActivity(r, customIntent); 
  26.             } 

7、接着调用performLaunchActivity

  1. private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { 
  2.         ActivityInfo aInfo = r.activityInfo; 
  3.         if (r.packageInfo == null) { 
  4.             r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo, 
  5.                     Context.CONTEXT_INCLUDE_CODE); 
  6.         } 
  7.         ComponentName component = r.intent.getComponent(); 
  8.         if (component == null) { 
  9.             component = r.intent.resolveActivity( 
  10.                 mInitialApplication.getPackageManager()); 
  11.             r.intent.setComponent(component); 
  12.         } 
  13.         if (r.activityInfo.targetActivity != null) { 
  14.             component = new ComponentName(r.activityInfo.packageName, 
  15.                     r.activityInfo.targetActivity); 
  16.         } 
  17. //创建Activity的context实例 
  18.         ContextImpl appContext = createBaseContextForActivity(r); 
  19.         Activity activity = null
  20.         try { 
  21.             java.lang.ClassLoader cl = appContext.getClassLoader(); 
  22.             //创建Activity的实例 
  23.             activity = mInstrumentation.newActivity( 
  24.                     cl, component.getClassName(), r.intent); 
  25.             StrictMode.incrementExpectedActivityCount(activity.getClass()); 
  26.             r.intent.setExtrasClassLoader(cl); 
  27.             r.intent.prepareToEnterProcess(); 
  28.             if (r.state != null) { 
  29.                 r.state.setClassLoader(cl); 
  30.             } 
  31.         } catch (Exception e) { 
  32.         } 
  33.         try { 
  34.             Application app = r.packageInfo.makeApplication(false, mInstrumentation); 
  35.             if (localLOGV) Slog.v(TAG, "Performing launch of " + r); 
  36.             if (localLOGV) Slog.v( 
  37.                     TAG, r + ": app=" + app 
  38.                     + ", appName=" + app.getPackageName() 
  39.                     + ", pkg=" + r.packageInfo.getPackageName() 
  40.                     + ", comp=" + r.intent.getComponent().toShortString() 
  41.                     + ", dir=" + r.packageInfo.getAppDir()); 
  42.             if (activity != null) { 
  43.                //获取Resources资源 
  44.                 appContext.getResources().addLoaders( 
  45.                         app.getResources().getLoaders().toArray(new ResourcesLoader[0])); 
  46.                 appContext.setOuterContext(activity); 
  47.                 activity.attach(appContext, this, getInstrumentation(), r.token, 
  48.                         r.ident, app, r.intent, r.activityInfo, title, r.parent, 
  49.                         r.embeddedID, r.lastNonConfigurationInstances, config, 
  50.                         r.referrer, r.voiceInteractor, window, r.configCallback, 
  51.                         r.assistToken); 
  52.                 if (r.isPersistable()) { 
  53.                 //回调Activity的onCreat 
  54.                     mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState); 
  55.                 } else { 
  56.                     mInstrumentation.callActivityOnCreate(activity, r.state); 
  57.                 } 
  58.             } 
  59.         } catch (SuperNotCalledException e) { 
  60.             throw e; 
  61.         } catch (Exception e) { 
  62.         } 
  63.         return activity; 
  64.     } 

在performLaunchActivity中,主要是加载App的资源包,然后创建了Activity的context实例,并创建了Activity的实例,接着调用activity.attach方法,attach执行完之后调用了onCreate方法。

8、APP的activity.attach

  1. activity.attach 
  2. final void attach(Context context, ActivityThread aThread, 
  3.             Instrumentation instr, IBinder token, int ident, 
  4.             Application application, Intent intent, ActivityInfo info, 
  5.             CharSequence title, Activity parent, String id, 
  6.             NonConfigurationInstances lastNonConfigurationInstances, 
  7.             Configuration config, String referrer, IVoiceInteractor voiceInteractor, 
  8.             Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) { 
  9.         attachBaseContext(context); 
  10.         mFragments.attachHost(null /*parent*/); 
  11.         //创建了PhoneWindow实例 
  12.         mWindow = new PhoneWindow(this, window, activityConfigCallback); 
  13.         //设置了WindowControllerCallback监听 
  14.         mWindow.setWindowControllerCallback(this); 
  15.         //设置了WindowCallback监听 
  16.         mWindow.setCallback(this); 
  17.         mWindow.setOnWindowDismissedCallback(this); 
  18.         mWindow.getLayoutInflater().setPrivateFactory(this); 
  19.         if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { 
  20.             mWindow.setSoftInputMode(info.softInputMode); 
  21.         } 
  22.         if (info.uiOptions != 0) { 
  23.             mWindow.setUiOptions(info.uiOptions); 
  24.         } 
  25.         //线程对象赋值,即ActivityThread main方法所在的线程 
  26.         mUiThread = Thread.currentThread(); 
  27. //初始化Activity的成员变量,包括windowmanager 
  28.         mMainThread = aThread; 
  29.         mInstrumentation = instr; 
  30.         mToken = token; 
  31.         mAssistToken = assistToken; 
  32.         mIdent = ident; 
  33.         mApplication = application; 
  34.         mIntent = intent; 
  35.         mReferrer = referrer; 
  36.         mComponent = intent.getComponent(); 
  37.         mActivityInfo = info; 
  38.         mTitle = title; 
  39.         mParent = parent; 
  40.         mEmbeddedID = id; 
  41.         mLastNonConfigurationInstances = lastNonConfigurationInstances; 
  42.         if (voiceInteractor != null) { 
  43.             if (lastNonConfigurationInstances != null) { 
  44.                 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor; 
  45.             } else { 
  46.                 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, 
  47.                         Looper.myLooper()); 
  48.             } 
  49.         } 
  50.         mWindow.setWindowManager( 
  51.                 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), 
  52.                 mToken, mComponent.flattenToString(), 
  53.                 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0); 
  54.         if (mParent != null) { 
  55.             mWindow.setContainer(mParent.getWindow()); 
  56.         } 
  57.         mWindowManager = mWindow.getWindowManager(); 
  58.         mCurrentConfig = config; 
  59.         mWindow.setColorMode(info.colorMode); 
  60.         setAutofillOptions(application.getAutofillOptions()); 
  61.         setContentCaptureOptions(application.getContentCaptureOptions()); 
  62.     } 

activity.attach中主要

1.创建了PhoneWindow实例

2.设置了Window接口的监听

3.初始化了成员变量,包括线程和WindowManager

三、点击桌面图标启动

当我们点击手机桌面上的图标的时候,App就由Launcher开始启动了。但是,你有没有思考过Launcher到底是一个什么东西?

Launcher本质上也是一个应用程序,和我们的App一样,也是继承自Activity

  1. packages/apps/Launcher2/src/com/android/launcher2/Launcher.java 
  2. public final class Launcher extends Activity 
  3.         implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, 
  4.                    View.OnTouchListener { 
  5.                    } 

auncher实现了点击、长按等回调接口,来接收用户的输入。既然是普通的App,那么我们的开发经验在这里就仍然适用,比如,我们点击图标的时候,是怎么开启的应用呢?如果让你,你怎么做这个功能呢?捕捉图标点击事件,然后startActivity()发送对应的Intent请求呗!是的,Launcher也是这么做的,就是这么easy!

那么到底是处理的哪个对象的点击事件呢?既然Launcher是App,并且有界面,那么肯定有布局文件呀,是的,我找到了布局文件launcher.xml

  1. <FrameLayout 
  2.     xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher" 
  4.     android:id="@+id/launcher"
  5.     <com.android.launcher2.DragLayer 
  6.         android:id="@+id/drag_layer" 
  7.         android:layout_width="match_parent" 
  8.         android:layout_height="match_parent" 
  9.         android:fitsSystemWindows="true"
  10.         <!-- Keep these behind the workspace so that they are not visible when 
  11.              we go into AllApps --> 
  12.         <include 
  13.             android:id="@+id/dock_divider" 
  14.             layout="@layout/workspace_divider" 
  15.             android:layout_marginBottom="@dimen/button_bar_height" 
  16.             android:layout_gravity="bottom" /> 
  17.         <include 
  18.             android:id="@+id/paged_view_indicator" 
  19.             layout="@layout/scroll_indicator" 
  20.             android:layout_gravity="bottom" 
  21.             android:layout_marginBottom="@dimen/button_bar_height" /> 
  22.         <!-- The workspace contains 5 screens of cells --> 
  23.         <com.android.launcher2.Workspace 
  24.             android:id="@+id/workspace" 
  25.             android:layout_width="match_parent" 
  26.             android:layout_height="match_parent" 
  27.             android:paddingStart="@dimen/workspace_left_padding" 
  28.             android:paddingEnd="@dimen/workspace_right_padding" 
  29.             android:paddingTop="@dimen/workspace_top_padding" 
  30.             android:paddingBottom="@dimen/workspace_bottom_padding" 
  31.             launcher:defaultScreen="2" 
  32.             launcher:cellCountX="@integer/cell_count_x" 
  33.             launcher:cellCountY="@integer/cell_count_y" 
  34.             launcher:pageSpacing="@dimen/workspace_page_spacing" 
  35.             launcher:scrollIndicatorPaddingLeft="@dimen/workspace_divider_padding_left" 
  36.             launcher:scrollIndicatorPaddingRight="@dimen/workspace_divider_padding_right"
  37.             <include android:id="@+id/cell1" layout="@layout/workspace_screen" /> 
  38.             <include android:id="@+id/cell2" layout="@layout/workspace_screen" /> 
  39.             <include android:id="@+id/cell3" layout="@layout/workspace_screen" /> 
  40.             <include android:id="@+id/cell4" layout="@layout/workspace_screen" /> 
  41.             <include android:id="@+id/cell5" layout="@layout/workspace_screen" /> 
  42.         </com.android.launcher2.Workspace> 
  43.     ...ignore some code... 
  44.     </com.android.launcher2.DragLayer> 
  45. </FrameLayout> 

程序列表界面,点击图标的时候会发生什么呢?实际上,程序列表界面使用的是AppsCustomizePagedView对象,所以我在这个类里面找到了onClick(View v)。

  1. com.android.launcher2.AppsCustomizePagedView.java 
  2. /** 
  3.  * The Apps/Customize page that displays all the applications, widgets, and shortcuts. 
  4.  */ 
  5. public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements 
  6.         View.OnClickListener, View.OnKeyListener, DragSource, 
  7.         PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener, 
  8.         LauncherTransitionable { 
  9.      @Override 
  10.     public void onClick(View v) { 
  11.          ...ignore some code... 
  12.         if (v instanceof PagedViewIcon) { 
  13.             mLauncher.updateWallpaperVisibility(true); 
  14.             mLauncher.startActivitySafely(v, appInfo.intent, appInfo); 
  15.         } else if (v instanceof PagedViewWidget) { 
  16.                  ...ignore some code.. 
  17.          } 
  18.      }       
  19.    } 

可以看到,调用的是

  1. mLauncher.startActivitySafely(v, appInfo.intent, appInfo); 

和上面一样!这叫什么?这叫殊途同归!

所以咱们现在又明白了一件事情:不管从哪里点击图标,调用的都是Launcher.startActivitySafely()。

下面我们就可以一步步的来看一下Launcher.startActivitySafely()到底做了什么事情。

  1. boolean startActivitySafely(View v, Intent intent, Object tag) { 
  2.         boolean success = false
  3.         try { 
  4.             success = startActivity(v, intent, tag); 
  5.         } catch (ActivityNotFoundException e) { 
  6.             Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); 
  7.             Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e); 
  8.         } 
  9.         return success; 
  10.     } 
  11. 调用了startActivity(v, intent, tag) 
  12.  boolean startActivity(View v, Intent intent, Object tag) { 
  13.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  14.         try { 
  15.             boolean useLaunchAnimation = (v != null) && 
  16.                     !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION); 
  17.             if (useLaunchAnimation) { 
  18.                 if (user == null || user.equals(android.os.Process.myUserHandle())) { 
  19.                     startActivity(intent, opts.toBundle()); 
  20.                 } else { 
  21.                     launcherApps.startMainActivity(intent.getComponent(), user
  22.                             intent.getSourceBounds(), 
  23.                             opts.toBundle()); 
  24.                 } 
  25.             } else { 
  26.                 if (user == null || user.equals(android.os.Process.myUserHandle())) { 
  27.                     startActivity(intent); 
  28.                 } else { 
  29.                     launcherApps.startMainActivity(intent.getComponent(), user
  30.                             intent.getSourceBounds(), null); 
  31.                 } 
  32.             } 
  33.             return true
  34.         } catch (SecurityException e) { 
  35.         ... 
  36.         } 
  37.         return false
  38.     } 

这里会调用Activity.startActivity(intent, opts.toBundle()),这个方法熟悉吗?这就是我们经常用到的Activity.startActivity(Intent)的重载函数。而且由于设置了

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

所以这个Activity会添加到一个新的Task栈中,而且,startActivity()调用的其实是startActivityForResult()这个方法。

  1. @Override 
  2.     public void startActivity(Intent intent, @Nullable Bundle options) { 
  3.         if (options != null) { 
  4.             startActivityForResult(intent, -1, options); 
  5.         } else { 
  6.             // Note we want to go through this call for compatibility with 
  7.             // applications that may have overridden the method. 
  8.             startActivityForResult(intent, -1); 
  9.         } 
  10.     } 

所以我们现在明确了,Launcher中开启一个App,其实和我们在Activity中直接startActivity()基本一样,都是调用了Activity.startActivityForResult()。

下次我们详细介绍下startActivity之后的流程

总结:

  • 点击桌面App图标,Launcher进程采用Binder IPC向system_server进程发起startActivity请求;
  • system_server进程接收到请求后,向zygote进程发送创建进程的请求;
  • Zygote进程fork出新的子进程,即App进程;
  • App进程,通过Binder IPC向sytem_server进程发起attachApplication请求;
  • system_server进程在收到请求后,进行一系列准备工作后,再通过binder IPC向App进程发送scheduleLaunchActivity请求;
  • App进程的binder线程(ApplicationThread)在收到请求后,通过handler向主线程发送LAUNCH_ACTIVITY消息;
  • 主线程在收到Message后,通过发射机制创建目标Activity,并回调Activity.onCreate()等方法

原文链接:https://mp.weixin.qq.com/s/V14dtw1Nkd0sK7wKT9ktiA