飞道的博客

Android系统启动过程分析

377人阅读  评论(0)

Android系统启动过程分析

一、Android平台架构

首先贴一张Android系统架构图方便理解整个Android架构,这可以让我们从整体上对整个启动流程有个大概认知。

可以看出整个架构由5部分构成,从下到上分别为:
1. Linux内核层
Android 的核心系统服务基于Linux 内核,在此基础上添加了部分Android专用的驱动。系统的安全性、内存管理、进程管理、网络协议栈和驱动模型等都依赖于该内核。
2. 硬件抽象层(HAL)
硬件抽象层是位于操作系统内核与硬件电路之间的接口层,其目的在于将硬件抽象化,为了保护硬件厂商的知识产权,它隐藏了特定平台的硬件接口细节,为操作系统提供虚拟硬件平台,使其具有硬件无关性,可在多种平台上进行移植。 HAL 由多个库模块组成,每个模块都为特定类型的硬件组件实现了一个接口,例如相机或蓝牙模块。 当框架 API 调用设备硬件时,Android 系统为该硬件组件加载库模块。
3. 系统运行库层(Native)
系统运行库层分为两部分,分别是C/C++程序库和Android运行时库。 C/C++程序库被Android中不同的部分使用 runtime库主要是Java核心库(提供了Java语言核心功能因此开发者可以使用Java编写应用)和ART(Android 5.0 之前是Dalvik)该虚拟机不同于JVM是专门用于移动设备的虚拟机 允许在有限的内存内运行多个虚拟机实例 并且每个应用都是独立的linux进程这样可以防止虚拟机崩溃时不会导致所有的进程被关闭。ART和Dalvik的区别是 后者在应用每次启动时才会通过即时编译把字节码转化为机器码 (这会影响应用运行效率)ART则是在应用第一次安装的时候就会把字节码文件转换为机器码 这样虽然会在安装时耗时增加 但app每次启动的时间会减少
4. 应用框架层(Java Framework)
应用框架层为开发人员提供了可以开发应用程序所需要的API,我们平常开发应用程序都是调用的这一层所提供的API,当然也包括系统的应用。这一层的是由Java代码编写的,可以称为Java Framework
该层包含以下内容:

  • 一个丰富和可扩展的视图系统
  • Resource Manager
  • Notification Manager
  • Activity Manager
  • Content Providers
  1. 应用层
    系统内置的应用程序以及非系统级的应用程序都是属于应用层。负责与用户进行直接交互,通常都是用Java进行开发的

二、Android系统的启动流程

Android系统的启动流程大致分为三个阶段:

  1. 电源键按下后加载引导程序Bootloader到RAM 然后Bootloader把OS拉起
  2. Linux 内核层面的启动
  3. Android系统层面的启动

本文主要分析Android系统层面的启动

一、Bootloader的加载

当电源键被按下后会去加载Bootloader,这个程序会把OS拉活,然后就会进入OS的启动过程。

二、Linux内核的启动

Android底层是基于Linux内核的并做了一些扩展,添加一些驱动比如binder。Linux内核的启动流程是先启动idle线程(整个Linux系统的初始线程 pid=0)然后idle线程会启动init和kthread线程。init线程是负责启动Android系统并且是Android系统其他进程的父进程。kthread线程则负责创建并管理Linux系统内核线程。

1、ildle进程

主要是完成Linux系统的启动然后完成一些初始化任务之后会进入一个死循环,不断判断是否有新的进程需要启动如果没有则会让cpu和系统时钟进入休眠状态 当有新进程需要创建时在唤醒cpu

2、kthread进程

主要负责创建Linux系统内核相关线程 并始终运行在内核线程中 作为内核线程中其他线程的父类 负责内核线程的调度和管理

3、init进程

它是Linux系统第一个用户进程,主要工作分为两部分。首先会完成内核的创建和初始化这部分内容跟Linux内核相关, 其次就是用户空间的创建和启动这部分内容跟Android系统的启动相关

init进程第一阶段做的主要工作是挂载分区,创建设备节点和一些关键目录,初始化日志输出系统,启用SELinux安全策略

init进程第二阶段主要工作是初始化属性系统,解析SELinux的匹配规则,处理子进程终止信号,启动系统属性服务,可以说每一项都很关键,如果说第一阶段是为属性系统,SELinux做准备,那么第二阶段就是真正去把这些落实的。

其实init进程在完成第二阶段工作的时候就已经涉及到Android系统层面的启动了 因为init进程会去加载init.rc配置文件然后启动Zygote进程。
Init的启动入口是在其main函数中


  
  1. int main(int argc, char** argv) {
  2. ......
  3. bool is_first_stage = (getenv( "INIT_SECOND_STAGE") == nullptr);
  4. if (is_first_stage) {
  5. boot_clock::time_point start_time = boot_clock::now();
  6. umask( 0);
  7. //创建和挂载启动所需要的文件目录
  8. mount( "tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
  9. mkdir( "/dev/pts", 0755);
  10. mkdir( "/dev/socket", 0755);
  11. mount( "devpts", "/dev/pts", "devpts", 0, NULL);
  12. #define MAKE_STR(x) __STRING(x)
  13. mount( "proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC));
  14. chmod( "/proc/cmdline", 0440);
  15. gid_t groups[] = { AID_READPROC };
  16. setgroups(arraysize(groups), groups);
  17. mount( "sysfs", "/sys", "sysfs", 0, NULL);
  18. mount( "selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL);
  19. mknod( "/dev/kmsg", S_IFCHR | 0600, makedev( 1, 11));
  20. mknod( "/dev/random", S_IFCHR | 0666, makedev( 1, 8));
  21. mknod( "/dev/urandom", S_IFCHR | 0666, makedev( 1, 9));
  22. ......
  23. // Set up SELinux, loading the SELinux policy.启动SELinux
  24. selinux_initialize( true);
  25. ......
  26. }
  27. ......
  28. //对属性服务进行初始化
  29. property_init();
  30. ......
  31. epoll_fd = epoll_create1(EPOLL_CLOEXEC);
  32. if (epoll_fd == -1) {
  33. PLOG(ERROR) << "epoll_create1 failed";
  34. exit( 1);
  35. }
  36. //用于设置子进程信号处理函数,如果子进程异常退出,init进程会调用该函
  37. //数中设置的信号处理函数进行处理。
  38. signal_handler_init();
  39. ......
  40. //启动属性服务
  41. start_property_service();
  42. ......
  43. if (bootscript.empty()) {
  44. //解析init.rc配置文件
  45. parser.ParseConfig( "/init.rc");
  46. parser.set_is_system_etc_init_loaded(
  47. parser.ParseConfig( "/system/etc/init"));
  48. parser.set_is_vendor_etc_init_loaded(
  49. parser.ParseConfig( "/vendor/etc/init"));
  50. parser.set_is_odm_etc_init_loaded(parser.ParseConfig( "/odm/etc/init"));
  51. }
  52. ......
  53. while ( true) {
  54. ......
  55. if (!(waiting_for_prop || ServiceManager::GetInstance().IsWaitingForExec())) {
  56. am.ExecuteOneCommand();
  57. }
  58. if (!(waiting_for_prop || ServiceManager::GetInstance().IsWaitingForExec())) {
  59. //重启死去的服务
  60. restart_processes();
  61. }
  62. ......
  63. }
  64. return 0;
  65. }

我们注意下init.rc文件,它的路径为:system/core/rootdir/init.rc。它是一个非常重要的配置文件,是由Android初始化语言编写的脚本。Android8.0对该文件进行来拆分,每个服务对应一个rc文件,启动Zygote的脚本在init.zygoteXX.rc中定义,代表多少位处理器。这里以64位为例。


  
  1. service zygote / system/bin/app_process64 -Xzygote / system/bin --zygote --start- system-server
  2. class main
  3. priority - 20
  4. user root
  5. group root readproc
  6. socket zygote stream 660 root system
  7. onrestart write /sys/android_power/request_state wake
  8. onrestart write /sys/power/ state on
  9. onrestart restart audioserver
  10. onrestart restart cameraserver
  11. onrestart restart media
  12. onrestart restart netd
  13. onrestart restart wificond
  14. writepid /dev/cpuset/foreground/tasks

我们只需要知道上面的命令就是创建名为zygote的进程即可。实际创建是通过app_process64完成的,对应的文件为app_main.cpp,创建完成后会调用zygote的mian函数。


  
  1. int main(int argc, char* const argv[])
  2. {
  3. ...
  4. AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv)); //1
  5. ...
  6. // Parse runtime arguments. Stop at first unrecognized option.
  7. bool zygote = false;
  8. bool startSystemServer = false;
  9. bool application = false;
  10. String8 niceName;
  11. String8 className;
  12. ++i; // Skip unused "parent dir" argument.
  13. while (i < argc) {
  14. const char* arg = argv[i++];
  15. if (strcmp(arg, "--zygote") == 0) {
  16. zygote = true; //2
  17. niceName = ZYGOTE_NICE_NAME;
  18. } else if (strcmp(arg, "--start-system-server") == 0) {
  19. startSystemServer = true;
  20. } else if (strcmp(arg, "--application") == 0) {
  21. application = true;
  22. } else if (strncmp(arg, "--nice-name=", 12) == 0) {
  23. niceName.setTo(arg + 12);
  24. } else if (strncmp(arg, "--", 2) != 0) {
  25. className.setTo(arg);
  26. break;
  27. } else {
  28. --i;
  29. break;
  30. }
  31. }
  32. ...
  33. if (zygote) {
  34. runtime.start( "com.android.internal.os.ZygoteInit", args, zygote); //3
  35. } else if (className) {
  36. runtime.start( "com.android.internal.os.RuntimeInit", args, zygote);
  37. } else {
  38. fprintf(stderr, "Error: no class name or --zygote supplied.\n");
  39. app_usage();
  40. LOG_ALWAYS_FATAL( "app_process: no class name or --zygote supplied.");
  41. }

首先在注释1处声明了一个AppRuntime类型的runtime,然后在注释2处将zygote置为true,最后在注释3处用runtime.start()启动zygote。

三、Android 系统的启动

之前我们跟踪到zygote进程的启动,zygote可以看做是Android系统所有进程的父进程,DVM和ART、应用程序进程以及运行系统的关键服务SystemServer进程都是由Zygote进程通过fork创建的。Zygote预加载初始化核心库类,让DVM和ART虚拟机共享代码、降低内存占用和启动时间,因此只要Zygote装载好了这些类和资源后,新的进程就不需要在装载这些类和资源了,它们共享Zygote进程的资源和类。

那接下来我们看下AppRuntime的start方法
frameworks/base/core/jni/AndroidRuntime.cpp


  
  1. void AndroidRuntime::start( const char* className, const Vector<String8>& options, bool zygote)
  2. {
  3. ...
  4. /* start the virtual machine */
  5. JniInvocation jni_invocation;
  6. jni_invocation.Init( NULL);
  7. JNIEnv* env;
  8. //1 启动Java虚拟机
  9. if (startVm(&mJavaVM, &env, zygote) != 0) {
  10. return;
  11. }
  12. onVmCreated(env);
  13. /*
  14. * Register android functions.
  15. *
  16. */
  17. if (startReg(env) < 0) { //2 注册jni方法
  18. ALOGE( "Unable to register all android natives\n");
  19. return;
  20. }
  21. /*
  22. * We want to call main() with a String array with arguments in it.
  23. * At present we have two arguments, the class name and an option string.
  24. * Create an array to hold them.
  25. */
  26. jclass stringClass;
  27. jobjectArray strArray;
  28. jstring classNameStr;
  29. stringClass = env->FindClass( " java /lang/String");
  30. assert(stringClass != NULL);
  31. strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
  32. assert(strArray != NULL);
  33. //从app_main传过来的参数classname值为:“com.android.internal.os.ZygoteInit”
  34. classNameStr = env->NewStringUTF(className);
  35. assert(classNameStr != NULL);
  36. env->SetObjectArrayElement(strArray, 0, classNameStr);
  37. for (size_t i = 0; i < options.size(); ++i) {
  38. jstring optionsStr = env->NewStringUTF(options.itemAt(i).string());
  39. assert(optionsStr != NULL);
  40. env->SetObjectArrayElement(strArray, i + 1, optionsStr);
  41. }
  42. /*
  43. * Start VM. This thread becomes the main thread of the VM, and will
  44. * not return until the VM exits.
  45. */
  46. char* slashClassName = toSlashClassName(className != NULL ? className : "");
  47. jclass startClass = env->FindClass(slashClassName);
  48. if (startClass == NULL) {
  49. ALOGE( "JavaVM unable to locate class '%s'\n", slashClassName);
  50. /* keep going */
  51. } else {
  52. jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
  53. "([Ljava/lang/String;)V"); //3 找到ZygoteInit的main函数,该函数是要启动的函数
  54. if (startMeth == NULL) {
  55. ALOGE( "JavaVM unable to find main() in '%s'\n", className);
  56. /* keep going */
  57. } else {
  58. env->CallStaticVoidMethod(startClass, startMeth, strArray); //4 通过Jni调用ZygoteInit的main函数
  59. #if 0
  60. if (env->ExceptionCheck())
  61. threadExitUncaughtException(env);
  62. #endif
  63. }
  64. }

在start方法中我们看到先是在注释1处启动虚拟机然后在注释2处注册jni.之后就是通过传入的类名找到要启动的class最终在注释3处将其赋值给startMeth(此时表示要启动ZygoteInit的main函数),最终的启动是在注释4处。

我们看下ZygoteInit的main函数


  
  1. public static void main(String argv[]) {
  2. ...
  3. ZygoteServer zygoteServer = new ZygoteServer(); //1 构造zygoteServer实例
  4. ...
  5. boolean startSystemServer = false;
  6. String socketName = "zygote";
  7. String abiList = null;
  8. boolean enableLazyPreload = false;
  9. for ( int i = 1; i < argv.length; i++) {
  10. if ( "start-system-server".equals(argv[i])) {
  11. startSystemServer = true;
  12. } else if ( "--enable-lazy-preload".equals(argv[i])) {
  13. enableLazyPreload = true;
  14. } else if (argv[i].startsWith(ABI_LIST_ARG)) {
  15. abiList = argv[i].substring(ABI_LIST_ARG.length());
  16. } else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
  17. socketName = argv[i].substring(SOCKET_NAME_ARG.length());
  18. } else {
  19. throw new RuntimeException( "Unknown command line argument: " + argv[i]);
  20. }
  21. }
  22. zygoteServer.registerServerSocket(socketName); //2 注册服务端的scoket
  23. if (startSystemServer) { //通过fork启动SystemServer进程
  24. Runnable r = forkSystemServer(abiList, socketName, zygoteServer); //3
  25. // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
  26. // child (system_server) process.
  27. if (r != null) {
  28. r.run(); //此处调用的run方法是MethodAndArgsCaller类的run 该类是RuntimeInit的一个静态内部类
  29. return;
  30. }
  31. }
  32. Log.i(TAG, "Accepting command socket connections");
  33. // The select loop returns early in the child process after a fork and
  34. // loops forever in the zygote.
  35. caller = zygoteServer.runSelectLoop(abiList); //4等待客户端请求 fork进程
  36. } catch (Throwable ex) {
  37. Log.e(TAG, "System zygote died with exception", ex);
  38. throw ex;
  39. } finally {
  40. zygoteServer.closeServerSocket();
  41. }

在注释1处构造了一个zygoteServer实例,之后在注释2处注册服务端的scoket,这里scoket的类型是loaclscoket它是Android对Linuxscoket的一种封装。Local Socket 是Linux提供的一种基于Socket的进程间通信方式,对Server端来讲,唯一的区别就是bind到一个本地的文件描述符(fd)而不是某个IP地址和端口号。Android里很多地方用到了Local Socket做进程间的通信。scoket创建完成后就等待用来相应客户端fork的请求,即在注释3处通过fork启动SystemServer进程,在注释4处等待客户端fork进程的请求。

总结来说ZygoteInit主要做了两件事:
1、注册了服务端的scoket
2、fork进程 启动SystemServer进程

此处说下zygoteinit这个类 Android8.0发生了一些改动。之前在main函数会先预加载(preload)一些东西然后注册服务端socket。在8.0版本预加载的操作放在了静态代码块中,下面是preload的静态代码块。preload主要做了两件事 preloadClasses(); preloadResources();
preloadClassess 将framework.jar里的preloaded-classes 定义的所有class load到内存里,preloaded-classes 编译Android后可以在framework/base下找到。而preloadResources 将系统的Resource(不是在用户apk里定义的resource)load到内存。

资源preload到Zygoted的进程地址空间,所有fork的子进程将共享这份空间而无需重新load, 这大大减少了应用程序的启动时间,但反过来增加了系统的启动时间。通过对preload 类和资源数目进行调整可以加快系统启动。


  
  1. static void preload(TimingsTraceLog bootTimingsTraceLog) {
  2. Log.d(TAG, "begin preload");
  3. bootTimingsTraceLog.traceBegin( "BeginIcuCachePinning");
  4. beginIcuCachePinning();
  5. bootTimingsTraceLog.traceEnd(); // BeginIcuCachePinning
  6. bootTimingsTraceLog.traceBegin( "PreloadClasses");
  7. preloadClasses();
  8. bootTimingsTraceLog.traceEnd(); // PreloadClasses
  9. bootTimingsTraceLog.traceBegin( "PreloadResources");
  10. preloadResources();
  11. bootTimingsTraceLog.traceEnd(); // PreloadResources
  12. Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadAppProcessHALs");
  13. nativePreloadAppProcessHALs();
  14. Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
  15. Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadOpenGL");
  16. preloadOpenGL();
  17. Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
  18. preloadSharedLibraries();
  19. preloadTextResources();
  20. // Ask the WebViewFactory to do any initialization that must run in the zygote process,
  21. // for memory sharing purposes.
  22. WebViewFactory.prepareWebViewInZygote();
  23. endIcuCachePinning();
  24. warmUpJcaProviders();
  25. Log.d(TAG, "end preload");
  26. sPreloadComplete = true;
  27. }

上面我们总结了ZygoteInit会先注册一个五福段的Scoket,我们来看下具体是如何注册的。


  
  1. void registerServerSocket( String socketName) {
  2. if (mServerSocket == null) {
  3. int fileDesc;
  4. final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName; //1
  5. try {
  6. String env = System.getenv(fullSocketName);
  7. fileDesc = Integer.parseInt(env);
  8. } catch (RuntimeException ex) {
  9. throw new RuntimeException(fullSocketName + " unset or invalid", ex);
  10. }
  11. try {
  12. FileDescriptor fd = new FileDescriptor();
  13. fd.setInt$(fileDesc);
  14. mServerSocket = new LocalServerSocket(fd); //2
  15. } catch (IOException ex) {
  16. throw new RuntimeException(
  17. "Error binding to local socket '" + fileDesc + "'", ex);
  18. }
  19. }
  20. }

注释1处的fullSocketName是该scoket的全名,在注释2处new了一个LocalServerSocket实例并传入文件描述符fd最后赋值给mServerSocket,它就是我们启动的scoket。当Zygote进程将SystemServer进程启动后, 就会在这个服务端的Socket上来等待ActivityManagerService请求Zygote进程来创建新的应用程序进程。

在创建完scoket之后便通过fork方式创建SystemServer进程,具体看main函数的注释3。最后在注释4处调用runSelectLoop函数。先看下注释3处是如何创建SystemServer进程,具体是通过forkSystemServer方法


  
  1. private static Runnable forkSystemServer(String abiList, String socketName,
  2. ZygoteServer zygoteServer) {
  3. /* Containers run without this capability, so avoid setting it in that case */
  4. if (!SystemProperties.getBoolean(PROPERTY_RUNNING_IN_CONTAINER, false)) {
  5. capabilities |= posixCapabilitiesAsBits(OsConstants.CAP_BLOCK_SUSPEND);
  6. }
  7. /* Hardcoded command line to start the system server
  8. */
  9. String args[] = {
  10. "--setuid=1000",
  11. "--setgid=1000",
  12. "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1023,1032,3001,3002,3003,3006,3007,3009,3010",
  13. "--capabilities=" + capabilities + "," + capabilities,
  14. "--nice-name=system_server", //代表进程名
  15. "--runtime-args",
  16. "com.android.server.SystemServer", //启动的类名
  17. };
  18. ZygoteConnection.Arguments parsedArgs = null;
  19. int pid;
  20. try {
  21. parsedArgs = new ZygoteConnection.Arguments(args);
  22. ZygoteConnection.applyDebuggerSystemProperty(parsedArgs); //1//将启动的参数解析成parsedArgs
  23. ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);
  24. /* Request to fork the system server process
  25. */
  26. pid = Zygote.forkSystemServer(
  27. parsedArgs.uid, parsedArgs.gid,
  28. parsedArgs.gids,
  29. parsedArgs.debugFlags,
  30. null,
  31. parsedArgs.permittedCapabilities,
  32. parsedArgs.effectiveCapabilities); //2 fork System Server进程
  33. } catch (IllegalArgumentException ex) {
  34. throw new RuntimeException(ex);
  35. }
  36. /* For child process
  37. */
  38. if (pid == 0) { //pid=0代表fork成功 并在子线程中启动system server进程
  39. if (hasSecondZygote(abiList)) {
  40. waitForSecondaryZygote(socketName);
  41. }
  42. zygoteServer.closeServerSocket();
  43. return handleSystemServerProcess(parsedArgs); //3启动system server进程 system server在这个函数进行初始化
  44. }
  45. return null;
  46. }

首先是构造一个字符串数组来设置用户和用户组id然后在注释1处将其解析成parsedArgs。在注释2处通过fork创建SystemServer进程并传入一些参数
启动system server进程,进程的最终创建是通过native方法nativeForkSystemServer完成的。
最后在注释3处启动了system server进程。

接下来看下注释4处的runSelectLoop作用 runSelectLoop函数如下所示


  
  1. Runnable runSelectLoop(String abiList) {
  2. ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
  3. ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
  4. //mServerSocket 就是之前注册的server scoket ,在这里得到它的fd并放到fd列表中
  5. fds. add(mServerSocket.getFileDescriptor());
  6. peers. add( null);
  7. while ( true) {
  8. //遍历Fds列表把其中的信息放到pollFds 中
  9. StructPollfd[] pollFds = new StructPollfd[fds.size()];
  10. for ( int i = 0; i < pollFds.length; ++i) {
  11. pollFds[i] = new StructPollfd();
  12. pollFds[i].fd = fds. get(i);
  13. pollFds[i].events = ( short) POLLIN;
  14. }
  15. try {
  16. Os.poll(pollFds, -1);
  17. } catch (ErrnoException ex) {
  18. throw new RuntimeException( "poll failed", ex);
  19. }
  20. for ( int i = pollFds.length - 1; i >= 0; --i) {
  21. if ((pollFds[i].revents & POLLIN) == 0) {
  22. continue;
  23. }
  24. //i=0表示服务端Socket与客户端连接上,也就是当前Zygote进程与ActivityManagerService建立了连接
  25. if (i == 0) {
  26. ZygoteConnection newPeer = acceptCommandPeer(abiList);
  27. peers. add(newPeer);
  28. //将ZygoteConnection的fd添加到fds列表中 以便接受AMS发送的创建应用的请求
  29. fds. add(newPeer.getFileDesciptor());
  30. } else {
  31. //如果i>0表示有创建新应用进程的请求
  32. try {
  33. ZygoteConnection connection = peers. get(i);
  34. //根据请求创建新的应用进程
  35. final Runnable command = connection.processOneCommand( this);
  36. if (mIsForkChild) {
  37. // We're in the child. We should always have a command to run at this
  38. // stage if processOneCommand hasn't called "exec".
  39. if (command == null) {
  40. throw new IllegalStateException( "command == null");
  41. }
  42. return command;
  43. } else {
  44. // We're in the server - we should never have any commands to run.
  45. if (command != null) {
  46. throw new IllegalStateException( "command != null");
  47. }
  48. // We don't know whether the remote side of the socket was closed or
  49. // not until we attempt to read from it from processOneCommand. This shows up as
  50. // a regular POLLIN event in our regular processing loop.
  51. if (connection.isClosedByPeer()) {
  52. connection.closeSocket();
  53. peers. remove(i);
  54. fds. remove(i);
  55. }
  56. }
  57. } catch (Exception e) {
  58. if (!mIsForkChild) {
  59. // We're in the server so any exception here is one that has taken place
  60. // pre-fork while processing commands or reading / writing from the
  61. // control socket. Make a loud noise about any such exceptions so that
  62. // we know exactly what failed and why.
  63. Slog.e(TAG, "Exception executing zygote command: ", e);
  64. // Make sure the socket is closed so that the other end knows immediately
  65. // that something has gone wrong and doesn't time out waiting for a
  66. // response.
  67. ZygoteConnection conn = peers. remove(i);
  68. conn.closeSocket();
  69. fds. remove(i);
  70. } else {
  71. // We're in the child so any exception caught here has happened post
  72. // fork and before we execute ActivityThread.main (or any other main()
  73. // method). Log the details of the exception and bring down the process.
  74. Log.e(TAG, "Caught post-fork exception in child process.", e);
  75. throw e;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }

新进程的创建是通过processOneCommand完成的。下面贴下processOneCommand函数


  
  1. Runnable processOneCommand(ZygoteServer zygoteServer) {
  2. ...
  3. //创建应用进程
  4. pid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid, parsedArgs.gids,
  5. parsedArgs.debugFlags, rlimits, parsedArgs.mountExternal, parsedArgs.seInfo,
  6. parsedArgs.niceName, fdsToClose, fdsToIgnore, parsedArgs.instructionSet,
  7. parsedArgs.appDataDir);
  8. ...

Zygote的forkAndSpecialize函数最终是通过native方法完成进程创建。


  
  1. int pid = nativeForkAndSpecialize(
  2. uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
  3. fdsToIgnore, instructionSet, appDataDir);

好了,到此init和zygote进程的创建和启动流程以及它过程中都做了哪些事我们都梳理了一遍。现在简单总结下:
1、init进程

  • 创建一些文件夹并挂载设备
  • 初始化和启动属性服务
  • 解析init.rc配置文件并启动zygote进程
    2、zygote进程
  • 创建VM
  • 创建并启动system server进程

zygote启动流程简单总结:首先通过AndroidRunTime.start来启动zygoteinit的main函数,在main函数中通过ZygoteServer注册了scoket并通过runSelectLoop进行监听AMS是否有启动新进程的请求,如果有就通过native方法创建新进程。并且创建了systemserver进程。

system server进程的启动

我们知道在zygote进程当中创建并启动system server进程。那么接下来我们一起看下system server进程启动过程都干了什么。

我们知道system server进程的启动是通过handleSystemServerProcess该函数位于com.android.internal.os.ZygoteInit


  
  1. private static Runnable handleSystemServerProcess(ZygoteConnection.Arguments parsedArgs) {
  2. ...
  3. if (parsedArgs.invokeWith != null) {
  4. String[] args = parsedArgs.remainingArgs;
  5. // If we have a non-null system server class path, we'll have to duplicate the
  6. // existing arguments and append the classpath to it. ART will handle the classpath
  7. // correctly when we exec a new process.
  8. if (systemServerClasspath != null) {
  9. String[] amendedArgs = new String[args.length + 2];
  10. amendedArgs[ 0] = "-cp";
  11. amendedArgs[ 1] = systemServerClasspath;
  12. System.arraycopy(args, 0, amendedArgs, 2, args.length);
  13. args = amendedArgs;
  14. }
  15. WrapperInit.execApplication(parsedArgs.invokeWith,
  16. parsedArgs.niceName, parsedArgs.targetSdkVersion,
  17. VMRuntime.getCurrentInstructionSet(), null, args);
  18. throw new IllegalStateException( "Unexpected return from WrapperInit.execApplication");
  19. } else {
  20. ClassLoader cl = null;
  21. if (systemServerClasspath != null) {
  22. cl = createPathClassLoader(systemServerClasspath, parsedArgs.targetSdkVersion);
  23. Thread.currentThread().setContextClassLoader(cl);
  24. }
  25. /*
  26. * Pass the remaining arguments to SystemServer.
  27. */
  28. return ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl); //1
  29. }
  30. }

可以看出最后调用ZygoteInit.zygoteInit


  
  1. public static final Runnable zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader) {
  2. if (RuntimeInit.DEBUG) {
  3. Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");
  4. }
  5. Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");
  6. RuntimeInit.redirectLogStreams();
  7. RuntimeInit.commonInit();
  8. ZygoteInit.nativeZygoteInit(); //调用native方法对Zygote进行init
  9. return RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader); //1
  10. }

ZygoteInit.zygoteInit调用RuntimeInit.applicationInit


  
  1. protected static Runnable applicationInit(int targetSdkVersion, String[] argv,
  2. ClassLoader classLoader) {
  3. ...
  4. nativeSetExitWithoutCleanup( true);
  5. // We want to be fairly aggressive about heap utilization, to avoid
  6. // holding on to a lot of memory that isn't needed.
  7. VMRuntime.getRuntime().setTargetHeapUtilization( 0.75f);
  8. VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);
  9. final Arguments args = new Arguments(argv);
  10. // The end of of the RuntimeInit event (see #zygoteInit).
  11. Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
  12. // Remaining arguments are passed to the start class's static main
  13. return findStaticMain(args.startClass, args.startArgs, classLoader); //1
  14. }

RuntimeInit.applicationInit又会调用findStaticMain方法


  
  1. private static Runnable findStaticMain(String className, String[] argv,
  2. ClassLoader classLoader) {
  3. Class<?> cl;
  4. try {
  5. cl = Class.forName(className, true, classLoader); //1
  6. } catch (ClassNotFoundException ex) {
  7. throw new RuntimeException(
  8. "Missing class when invoking static main " + className,
  9. ex);
  10. }
  11. Method m;
  12. try {
  13. m = cl.getMethod( "main", new Class[] { String[].class }); //2
  14. } catch (NoSuchMethodException ex) {
  15. throw new RuntimeException(
  16. "Missing static main on " + className, ex);
  17. } catch (SecurityException ex) {
  18. throw new RuntimeException(
  19. "Problem getting static main on " + className, ex);
  20. }
  21. int modifiers = m.getModifiers(); //3
  22. if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
  23. throw new RuntimeException(
  24. "Main method is not public and static on " + className);
  25. }
  26. /*
  27. * This throw gets caught in ZygoteInit.main(), which responds
  28. * by invoking the exception's run() method. This arrangement
  29. * clears up all the stack frames that were required in setting
  30. * up the process.
  31. */
  32. return new MethodAndArgsCaller(m, argv);
  33. }

注释2处根据传入的类名获取Class对象,注释2处则获取该类的main方法。注释3处获取main方法的修饰判断是否符合要求(必须是pubilc static)最终返回一个MethodAndArgsCaller对象。
它是RuntimeInit类的 一个静态内部类并且实现了runnable接口。


  
  1. static class MethodAndArgsCaller implements Runnable {
  2. /** method to call */
  3. private final Method mMethod;
  4. /** argument array */
  5. private final String[] mArgs;
  6. public MethodAndArgsCaller(Method method, String[] args) {
  7. mMethod = method;
  8. mArgs = args;
  9. }
  10. public void run() { //1
  11. try {
  12. mMethod.invoke( null, new Object[] { mArgs }); //2
  13. } catch (IllegalAccessException ex) {
  14. throw new RuntimeException(ex);
  15. } catch (InvocationTargetException ex) {
  16. Throwable cause = ex.getCause();
  17. if (cause instanceof RuntimeException) {
  18. throw (RuntimeException) cause;
  19. } else if (cause instanceof Error) {
  20. throw (Error) cause;
  21. }
  22. throw new RuntimeException(ex);
  23. }
  24. }
  25. }

首先看下在它的run方法中的注释2处执行了传进来的main方法,这个其实就是SystemServer的main方法。那么还剩一个问题是它的run函数(注释1)在哪里执行的呢? 你是否还记得ZygoteInit类的main函数中有如下调用


  
  1. public static void main(String argv[]) {
  2. ...
  3. if (startSystemServer) {
  4. Runnable r = forkSystemServer(abiList, socketName, zygoteServer); //创建SystemServer进程
  5. // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
  6. // child (system_server) process.
  7. if (r != null) {
  8. r.run(); //1
  9. return;
  10. }
  11. }

我们看forkSystemServer返回了一个runnable对象然后如果它不为空就调用它的run方法。其实这个runnable就是一个MethodAndArgsCaller实例。由此我们就进入到了SystemServer类的main方法中。


  
  1. /**
  2. * The main entry point from zygote.
  3. */
  4. public static void main(String[] args) {
  5. new SystemServer().run();
  6. }

该main方法非常简单就是调用自身的run方法。


  
  1. private void run() {
  2. try {
  3. //手机时间早于1970 会重新设置为1970
  4. if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
  5. Slog.w(TAG, "System clock is before 1970; setting to 1970.");
  6. SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
  7. }
  8. ...
  9. // Prepare the main looper thread (this thread).
  10. android.os.Process.setThreadPriority(
  11. android.os.Process.THREAD_PRIORITY_FOREGROUND);
  12. android.os.Process.setCanSelfBackground( false);
  13. Looper.prepareMainLooper(); //1准备主线程looper
  14. // Initialize native services.
  15. System.loadLibrary( "android_servers"); //2初始化native相关服务
  16. // Check whether we failed to shut down last time we tried.
  17. // This call may not return.
  18. performPendingShutdown();
  19. // Initialize the system context.
  20. createSystemContext(); //3初始化系统context
  21. // Create the system service manager.
  22. mSystemServiceManager = new SystemServiceManager(mSystemContext); //4创建system service manager
  23. mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
  24. LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
  25. // Prepare the thread pool for init tasks that can be parallelized
  26. SystemServerInitThreadPool. get();
  27. } finally {
  28. traceEnd(); // InitBeforeStartServices
  29. }
  30. // Start services.
  31. try {
  32. traceBeginAndSlog( "StartServices");
  33. //5启动各种服务
  34. startBootstrapServices();
  35. startCoreServices();
  36. startOtherServices();
  37. SystemServerInitThreadPool.shutdown();
  38. } catch (Throwable ex) {
  39. Slog.e( "System", "******************************************");
  40. Slog.e( "System", "************ Failure starting system services", ex);
  41. throw ex;
  42. } finally {
  43. traceEnd();
  44. }
  45. ...
  46. // Loop forever.
  47. Looper.loop(); //6
  48. throw new RuntimeException( "Main thread loop unexpectedly exited");

run方法信息量还是很大的,先看下注释1处初始化主线程的looper,注释2初始化了native相关服务,注释3初始化system context,注释4创建system service manager,注释5则先后初始化了各种服务,注释6调用了Looper.loop()。
注释3处system context其实最终是返回contextimpl实例。注释4处创建的SystemServiceManager主要是用在稍后初始化各种服务时用来启动的服务的。

一个Service启动需要的几个步骤:
1. 初始化Service 对象,获得IBinder对象。
2. 启动后台线程,并进入Loop等待。
3. 将自己注册到Service Manager, 让其他进程通过名字可以获得远程调用必须的IBinder的对象。
注释5处先后启动了多种系统服务,这些服务分为3类:引导服务、系统服务、其他服务。毫无疑问,这么多服务之间是有依赖关系的,比如说,ActivityManager Service 在WindowManager Service 初始化完成之前是不能启动应用的。那如何控制这些先后顺序的?这里由System server的启动线程通过SystemReady()接口来完成。每个系统服务必须实现一个SystemReady() 接口,当被调用,表明系统已经OK, 该服务可以访问(直接或间接)其他服务的资源。 最后一个被调到的服务就是AcitivyManager Service. AM的SystemReady()。

系统的启动方式有两种:

  • 第一种是通过SystemServiceManager.startService启动,下面以PowerManager启动为例:

  
  1. private void startBootstrapServices() {
  2. mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
  3. }

  
  1. public <T extends SystemService> T startService( Class<T> serviceClass) {
  2. try {
  3. try {
  4. Constructor<T> constructor = serviceClass.getConstructor(Context.class);
  5. service = constructor.newInstance(mContext);
  6. } catch (InstantiationException ex) {
  7. throw new RuntimeException( "Failed to create service " + name
  8. + ": service could not be instantiated", ex);
  9. } catch (IllegalAccessException ex) {
  10. throw new RuntimeException( "Failed to create service " + name
  11. + ": service must have a public constructor with a Context argument", ex);
  12. } catch (NoSuchMethodException ex) {
  13. throw new RuntimeException( "Failed to create service " + name
  14. + ": service must have a public constructor with a Context argument", ex);
  15. } catch (InvocationTargetException ex) {
  16. throw new RuntimeException( "Failed to create service " + name
  17. + ": service constructor threw an exception", ex);
  18. }
  19. startService(service); //1
  20. return service;
  21. } finally {
  22. Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
  23. }

可以看出首先会用反射得到一个service实例然后调用同名startService方法


  
  1. private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();
  2. public void startService(@NonNull final SystemService service) {
  3. // Register it.
  4. mServices.add(service); //1
  5. // Start it.
  6. long time = SystemClock.elapsedRealtime();
  7. try {
  8. service.onStart(); //2
  9. } catch (RuntimeException ex) {
  10. throw new RuntimeException( "Failed to start service " + service.getClass().getName()
  11. + ": onStart threw an exception", ex);
  12. }
  13. warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
  14. }

startService首先会把当前的service添加到一个名为mServices的list中
之后便调用service.onStart()来启动service。
以上就是通过SystemServiceManager.startService启动系统服务的过程,总结来说就是通过反射来构造service然后再调用其onStart()来启动。

  • 第二种启动系统服务的方式是通过调用服务的main方法启动即XXService.main(),以PackageManagerService为例来说明下

  
  1. mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
  2. mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);

调用了PackageManagerService的main函数


  
  1. public static PackageManagerService main(Context context, Installer installer,
  2. boolean factoryTest, boolean onlyCore) {
  3. // Self-check for initial settings.
  4. PackageManagerServiceCompilerMapping.checkProperties();
  5. PackageManagerService m = new PackageManagerService(context, installer,
  6. factoryTest, onlyCore); //1
  7. m.enableSystemUserPackages();
  8. ServiceManager.addService( "package", m); //2
  9. final PackageManagerNative pmn = m.new PackageManagerNative();
  10. ServiceManager.addService( "package_native", pmn);
  11. return m;
  12. }

在注释1处构造了一个 PackageManagerService实例,然后在注释2处添加到ServiceManager启动。
ServiceManager它是用来管理系统中各种Service的,由它管理的服务都实现了IBinder,所以在ServiceManager中注册的服务是用于进程间通信的:用于系统C/S架构中的Binder通信机制。客户端要使用某个Service,需要先到ServiceManager中查询Service的信息,然后根据该信息与Service所在的Server进程建立通信,这样客户端就可以使用Service了。

至此SystemServer的启动流程完成,我们来简单总结下:
1、zygote会通过forkSystemServe来创建SystemServer进程。
2、之后通过handleSystemServerProcess来启动SystemServer进程。
3、启动过程是通过反射调用SystemServer的main函数。
4、SystemServer的main函数会调用run方法,在run方法中初始化了主线程的looper、系统的context等,之后启动了引导、核心、其他等三类系统服务。

Launcher的启动

SystemServer的startOtherServices()方法的最后调用了AMS的systemReady
这个调用会启动launcher进程。
launcher通俗讲就是我们开机看到的桌面,它向我们展示安装完成的app的图标。并且当我们按下对应的图标时启动相应的app。好了 我们看下launcher启动入口。


  
  1. private void startOtherServices() {
  2. ...
  3. mActivityManagerService.systemReady(() -> { //1
  4. Slog.i(TAG, "Making services ready");
  5. traceBeginAndSlog( "StartActivityManagerReadyPhase");
  6. mSystemServiceManager.startBootPhase(
  7. SystemService.PHASE_ACTIVITY_MANAGER_READY);
  8. traceEnd();
  9. traceBeginAndSlog( "StartObservingNativeCrashes");
  10. try {
  11. mActivityManagerService.startObservingNativeCrashes();
  12. } catch (Throwable e) {
  13. reportWtf( "observing native crashes", e);
  14. }
  15. traceEnd();
  16. //...
  17. }, BOOT_TIMINGS_TRACE_LOG);
  18. }

在startOtherServices中注释1处调用ActivityManagerService(AMS)的systemReady方法。


  
  1. public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
  2. ...
  3. startHomeActivityLocked(currentUserId, "systemReady");
  4. ...
  5. }

省略无关内容,我们看在systemReady调用startHomeActivityLocked方法。


  
  1. boolean startHomeActivityLocked(int userId, String reason) {
  2. if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
  3. && mTopAction == null) {
  4. // We are running in factory test mode, but unable to find
  5. // the factory test app, so just sit around displaying the
  6. // error message and don't try to start anything.
  7. return false;
  8. }
  9. Intent intent = getHomeIntent(); //1
  10. ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
  11. if (aInfo != null) {
  12. intent.setComponent( new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
  13. // Don't do this if the home app is currently being
  14. // instrumented.
  15. aInfo = new ActivityInfo(aInfo);
  16. aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
  17. ProcessRecord app = getProcessRecordLocked(aInfo.processName,
  18. aInfo.applicationInfo.uid, true);
  19. if (app == null || app.instr == null) {
  20. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
  21. final int resolvedUserId = UserHandle.getUserId(aInfo.applicationInfo.uid);
  22. // For ANR debugging to verify if the user activity is the one that actually
  23. // launched.
  24. final String myReason = reason + ":" + userId + ":" + resolvedUserId;
  25. mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason); //2
  26. }
  27. } else {
  28. Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
  29. }
  30. return true;
  31. }

startHomeActivityLocked方法中在注释1处调用
getHomeIntent,该方法返回一个intent实例,该实例是启动launcher的intent。该intent的action和Category是匹配launcher的manifest的。


  
  1. Intent getHomeIntent() {
  2. Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null); //mTopAction = Intent.ACTION_MAIN
  3. intent.setComponent(mTopComponent);
  4. intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
  5. if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
  6. intent.addCategory(Intent.CATEGORY_HOME); //该intent的Category是Intent.CATEGORY_HOME
  7. }
  8. return intent;
  9. }

下面贴下manifest文件


  
  1. <application
  2. android:backupAgent= "com.android.launcher3.LauncherBackupAgent"
  3. android:fullBackupOnly= "true"
  4. android:fullBackupContent= "@xml/backupscheme"
  5. android:hardwareAccelerated= "true"
  6. android:icon= "@drawable/ic_launcher_home"
  7. android:label= "@string/derived_app_name"
  8. android:theme= "@style/LauncherTheme"
  9. android:largeHeap= "@bool/config_largeHeap"
  10. android:restoreAnyVersion= "true"
  11. android:supportsRtl= "true" >
  12. <!--
  13. Main launcher activity. When extending only change the name, and keep all the
  14. attributes and intent filters the same
  15. -->
  16. <activity
  17. android:name= "com.android.launcher3.Launcher"
  18. android:launchMode= "singleTask"
  19. android:clearTaskOnLaunch= "true"
  20. android:stateNotNeeded= "true"
  21. android:windowSoftInputMode= "adjustPan"
  22. android:screenOrientation= "nosensor"
  23. android:configChanges= "keyboard|keyboardHidden|navigation"
  24. android:resizeableActivity= "true"
  25. android:resumeWhilePausing= "true"
  26. android:taskAffinity= ""
  27. android:enabled= "true">
  28. <intent-filter>
  29. <action android:name="android.intent.action.MAIN" />
  30. <category android:name="android.intent.category.HOME" />
  31. <category android:name="android.intent.category.DEFAULT" />
  32. <category android:name="android.intent.category.MONKEY"/>
  33. <category android:name="android.intent.category.LAUNCHER_APP" />
  34. </intent-filter>
  35. </activity>

看出launcher的action和category是和上面的intent相符合的。

startHomeActivityLocked方法中的注释2调用了startHomeActivityLocked方法把获得的intent传入来启动launcher。
launcher启动完成后我们就在看到了桌面程序,上面显示了安装完成的app的图标。

我们在此简单总结下launcher的启动流程:
1、首先systemserver在启动其他系统服务时会调用AMS的systemReady方法
2、systemReady方法中调用startHomeActivityLocked,在这个方法中我们会构造出启动launcher的intent,然后通过startHomeActivityLocked去启动launcher。
systemserver启动launcher后会执行launcher的onCreate函数


  
  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. LauncherAppState app = LauncherAppState.getInstance( this); //1
  4. // Load configuration-specific DeviceProfile
  5. mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile( this);
  6. if (isInMultiWindowModeCompat()) { //2
  7. Display display = getWindowManager().getDefaultDisplay();
  8. Point mwSize = new Point();
  9. display.getSize(mwSize);
  10. mDeviceProfile = mDeviceProfile.getMultiWindowProfile( this, mwSize);
  11. }
  12. //3
  13. mOrientation = getResources().getConfiguration().orientation;
  14. mSharedPrefs = Utilities.getPrefs( this);
  15. mIsSafeModeEnabled = getPackageManager().isSafeMode();
  16. mModel = app.setLauncher( this);
  17. mModelWriter = mModel.getWriter(mDeviceProfile.isVerticalBarLayout());
  18. mIconCache = app.getIconCache();
  19. mAccessibilityDelegate = new LauncherAccessibilityDelegate( this);
  20. mDragController = new DragController( this);
  21. mAllAppsController = new AllAppsTransitionController( this);
  22. mStateTransitionAnimation = new LauncherStateTransitionAnimation( this, mAllAppsController);
  23. mAppWidgetManager = AppWidgetManagerCompat.getInstance( this);
  24. mAppWidgetHost = new LauncherAppWidgetHost( this);
  25. if (Utilities.ATLEAST_MARSHMALLOW) {
  26. mAppWidgetHost.addProviderChangeListener( this);
  27. }
  28. mAppWidgetHost.startListening();
  29. // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
  30. // this also ensures that any synchronous binding below doesn't re-trigger another
  31. // LauncherModel load.
  32. mPaused = false;
  33. mLauncherView = LayoutInflater.from( this).inflate(R.layout.launcher, null);
  34. setupViews(); //4
  35. mDeviceProfile.layout( this, false /* notifyListeners */);
  36. loadExtractedColorsAndColorItems(); //5
  37. mPopupDataProvider = new PopupDataProvider( this);
  38. ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
  39. .addAccessibilityStateChangeListener( this);
  40. lockAllApps();
  41. restoreState(savedInstanceState);
  42. if (LauncherAppState.PROFILE_STARTUP) {
  43. Trace.endSection();
  44. }
  45. // We only load the page synchronously if the user rotates (or triggers a
  46. // configuration change) while launcher is in the foreground
  47. int currentScreen = PagedView.INVALID_RESTORE_PAGE;
  48. if (savedInstanceState != null) {
  49. currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
  50. }
  51. if (!mModel.startLoader(currentScreen)) { //6
  52. // If we are not binding synchronously, show a fade in animation when
  53. // the first page bind completes.
  54. mDragLayer.setAlpha( 0);
  55. } else {
  56. // Pages bound synchronously.
  57. mWorkspace.setCurrentPage(currentScreen);
  58. setWorkspaceLoading( true);
  59. }
  60. // For handling default keys
  61. //7
  62. mDefaultKeySsb = new SpannableStringBuilder();
  63. Selection.setSelection(mDefaultKeySsb, 0);
  64. mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
  65. // In case we are on a device with locked rotation, we should look at preferences to check
  66. // if the user has specifically allowed rotation.
  67. if (!mRotationEnabled) {
  68. mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
  69. mRotationPrefChangeHandler = new RotationPrefChangeHandler();
  70. mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
  71. }
  72. if (PinItemDragListener.handleDragRequest( this, getIntent())) {
  73. // Temporarily enable the rotation
  74. mRotationEnabled = true;
  75. }
  76. // On large interfaces, or on devices that a user has specifically enabled screen rotation,
  77. // we want the screen to auto-rotate based on the current orientation
  78. setOrientation();
  79. setContentView(mLauncherView);
  80. // Listen for broadcasts
  81. IntentFilter filter = new IntentFilter();
  82. filter.addAction(Intent.ACTION_SCREEN_OFF);
  83. filter.addAction(Intent.ACTION_USER_PRESENT); // When the device wakes up + keyguard is gone
  84. registerReceiver(mReceiver, filter);
  85. mShouldFadeInScrim = true;
  86. getSystemUiController().updateUiState(SystemUiController.UI_STATE_BASE_WINDOW,
  87. Themes.getAttrBoolean( this, R.attr.isWorkspaceDarkText));
  88. if (mLauncherCallbacks != null) {
  89. mLauncherCallbacks.onCreate(savedInstanceState);
  90. }
  91. }

整个launcher的启动大致分为7步。
注释1处执行第一步创建LauncherAppState 对象。不同的手机显示的Launcher布局是一样的,但是其中真正显示的每个图标,
每个画面的像素点大小是不同的。Launcher需要根据手机的尺寸密度等参数,计算出更多的信息。第一步是将和手机硬件挂钩的参数都获取出来。

注释2处执行第二步,分屏模式也叫做多屏模式,在多屏模式的时候,Launcher的布局有很多的变化。此处检查当前是否处于分屏模式,若是则会做相应的处理。

注释3执行第三步统一创建对象,Launcher启动时需要用到的对象,在这里统一创建,为后面进行布局的显示进行铺垫。

注释4执行第四步生成桌面分布局,将桌面的各个部分都创建对象,绑定一些事件监听器等,这一步基本将桌面的各个UI子模块都定义完成。

注释5执行第五步,UI子模块的细节规划,各个模块的大小,真正的尺寸等等。这一步是采用第一步获取的方案,把第四步的模块细节进行完成

注释6执行第六步,生成布局。Launcher不是一张图片,因为不同的手机之间有区别。前五步完成不同手机的区别, 保证上至平板,下至翻盖机,不同的分辨率下都能够很好的显示。而手机桌面的变化重点是桌面图标布局不一样,手机中安装的软件不一样。第六步就是生成这种布局。

注释7第七步,横屏和CallBack等善后工作

至此launcher启动完成我们已经可以在桌面看到安装的app的icon,点击icon就会启动相应的应用。

其实在launcher内部我们看到的图标排列是通过Android自定义的recycleview实现的,当我们点击icon时会触发onclicklistener监听,而对点击事件的处理则是在launcher的onclick函数中(launcher实现了onclicklistener接口),launcher收到点击事件后会调用startAppShortcutOrInfoActivity启动对应的应用。

至此Android从启动到桌面显示,然后点击icon启动app的流程已经梳理完成。


转载:https://blog.csdn.net/songfei_dream/article/details/103927029
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场