当前位置:   article > 正文

Android应用跟系统之间通信什么时候用Socket什么时候用Binder结合源码详解_android开发binder和socket使用时机

android开发binder和socket使用时机

讲解这个问题之前我们先来看看这个问题“为什么进程间不能直接进行通信,而需要借助Binder或者Socket通信”, 其实这个问题只要了解过虚拟机的同学大概都会明白怎么回事:

由于在安卓系统中一个进程对应一个虚拟机实例,而在Java虚拟机实例中只存在一个堆区和一个方法区,并且被所有线程共享,所以能够直接通信的只有在虚拟机内部堆区和方法区,进程间通信属于虚拟机对象间通信不能直接通信,当然大家还应该明白一个机器只有一个虚拟机创建多个虚拟机实例来实现多进程也就是多应用。至于栈区、堆区、方法区各自存储的是什么,这就需要同学们认真的去了解一下虚拟机,这里就不做过多详解了。

Android进程流程图

好了言归正传,再回来聊聊为什么Android进程间通信有时候用Binder有时候用Socket,其实这也是跟Java虚拟机有关的。安卓系统中进程间通信使用Socket通信的,system_server进程与zygote进程通信的时候用到了Socket,system_server进程通过LocalSocket连接zygote进程的serverSocket传递参数ActivityThread,通知zygote进程fork出Android应用进程执行ActivityThread的main()方法。由于zygote进程是所有Android应用进程的父进程,system_server进程也是由zygote进程fork出来的大儿子基于Dalvik(ART) VM虚拟机实例,zygote进程虚拟机实例也是Dalvik(ART),所以zygote进程与system_server进程是父子关系只能使用socket通信,Binder通信属于基于Dalvik(ART) VM虚拟机两个进程兄弟之间的通信方式。那我们就看一下zygote源码为什么说父子进程间只能socket通信,首先看看ZygoteInit的main方法:

  1. public static void main(String argv[]) {
  2. ZygoteServer zygoteServer = new ZygoteServer();
  3. // Mark zygote start. This ensures that thread creation will throw
  4. // an error.
  5. ZygoteHooks.startZygoteNoThreadCreation();
  6. // Zygote goes into its own process group.
  7. try {
  8. Os.setpgid(0, 0);
  9. } catch (ErrnoException ex) {
  10. throw new RuntimeException("Failed to setpgid(0,0)", ex);
  11. }
  12. final Runnable caller;
  13. try {
  14. // Report Zygote start time to tron unless it is a runtime restart
  15. if (!"1".equals(SystemProperties.get("sys.boot_completed"))) {
  16. MetricsLogger.histogram(null, "boot_zygote_init",
  17. (int) SystemClock.elapsedRealtime());
  18. }
  19. String bootTimeTag = Process.is64Bit() ? "Zygote64Timing" : "Zygote32Timing";
  20. TimingsTraceLog bootTimingsTraceLog = new TimingsTraceLog(bootTimeTag,
  21. Trace.TRACE_TAG_DALVIK);
  22. bootTimingsTraceLog.traceBegin("ZygoteInit");
  23. RuntimeInit.enableDdms();
  24. boolean startSystemServer = false;
  25. String socketName = "zygote";
  26. String abiList = null;
  27. boolean enableLazyPreload = false;
  28. for (int i = 1; i < argv.length; i++) {
  29. if ("start-system-server".equals(argv[i])) {
  30. startSystemServer = true;
  31. } else if ("--enable-lazy-preload".equals(argv[i])) {
  32. enableLazyPreload = true;
  33. } else if (argv[i].startsWith(ABI_LIST_ARG)) {
  34. abiList = argv[i].substring(ABI_LIST_ARG.length());
  35. } else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
  36. socketName = argv[i].substring(SOCKET_NAME_ARG.length());
  37. } else {
  38. throw new RuntimeException("Unknown command line argument: " + argv[i]);
  39. }
  40. }
  41. if (abiList == null) {
  42. throw new RuntimeException("No ABI list supplied.");
  43. }
  44. zygoteServer.registerServerSocketFromEnv(socketName);
  45. // In some configurations, we avoid preloading resources and classes eagerly.
  46. // In such cases, we will preload things prior to our first fork.
  47. if (!enableLazyPreload) {
  48. bootTimingsTraceLog.traceBegin("ZygotePreload");
  49. EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
  50. SystemClock.uptimeMillis());
  51. preload(bootTimingsTraceLog);
  52. EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
  53. SystemClock.uptimeMillis());
  54. bootTimingsTraceLog.traceEnd(); // ZygotePreload
  55. } else {
  56. Zygote.resetNicePriority();
  57. }
  58. // Do an initial gc to clean up after startup
  59. bootTimingsTraceLog.traceBegin("PostZygoteInitGC");
  60. gcAndFinalize();
  61. bootTimingsTraceLog.traceEnd(); // PostZygoteInitGC
  62. bootTimingsTraceLog.traceEnd(); // ZygoteInit
  63. // Disable tracing so that forked processes do not inherit stale tracing tags from
  64. // Zygote.
  65. Trace.setTracingEnabled(false, 0);
  66. Zygote.nativeSecurityInit();
  67. // Zygote process unmounts root storage spaces.
  68. Zygote.nativeUnmountStorageOnInit();
  69. ZygoteHooks.stopZygoteNoThreadCreation();
  70. if (startSystemServer) {
  71. Runnable r = forkSystemServer(abiList, socketName, zygoteServer);
  72. // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
  73. // child (system_server) process.
  74. if (r != null) {
  75. r.run();
  76. return;
  77. }
  78. }
  79. Log.i(TAG, "Accepting command socket connections");
  80. // The select loop returns early in the child process after a fork and
  81. // loops forever in the zygote.
  82. caller = zygoteServer.runSelectLoop(abiList);
  83. } catch (Throwable ex) {
  84. Log.e(TAG, "System zygote died with exception", ex);
  85. throw ex;
  86. } finally {
  87. zygoteServer.closeServerSocket();
  88. }
  89. // We're in the child process and have exited the select loop. Proceed to execute the
  90. // command.
  91. if (caller != null) {
  92. caller.run();
  93. }
  94. }

我们可以看到main方法中先是zygoteServer.registerServerSocketFromEnv(socketName);创建ServerSocket,接着preload(bootTimingsTraceLog);预加载基础类、资源等数据提供后续forkSystemServer(abiList, socketName, zygoteServer)复制进程所需数据,那么我们再看看SystemServer的main方法:

  1. public static void main(String[] args) {
  2. new SystemServer().run();
  3. }
  4. private void run() {
  5. try {
  6. traceBeginAndSlog("InitBeforeStartServices");
  7. // If a device's clock is before 1970 (before 0), a lot of
  8. // APIs crash dealing with negative numbers, notably
  9. // java.io.File#setLastModified, so instead we fake it and
  10. // hope that time from cell towers or NTP fixes it shortly.
  11. if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
  12. Slog.w(TAG, "System clock is before 1970; setting to 1970.");
  13. SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
  14. }
  15. //
  16. // Default the timezone property to GMT if not set.
  17. //
  18. String timezoneProperty = SystemProperties.get("persist.sys.timezone");
  19. if (timezoneProperty == null || timezoneProperty.isEmpty()) {
  20. Slog.w(TAG, "Timezone not set; setting to GMT.");
  21. SystemProperties.set("persist.sys.timezone", "GMT");
  22. }
  23. // If the system has "persist.sys.language" and friends set, replace them with
  24. // "persist.sys.locale". Note that the default locale at this point is calculated
  25. // using the "-Duser.locale" command line flag. That flag is usually populated by
  26. // AndroidRuntime using the same set of system properties, but only the system_server
  27. // and system apps are allowed to set them.
  28. //
  29. // NOTE: Most changes made here will need an equivalent change to
  30. // core/jni/AndroidRuntime.cpp
  31. if (!SystemProperties.get("persist.sys.language").isEmpty()) {
  32. final String languageTag = Locale.getDefault().toLanguageTag();
  33. SystemProperties.set("persist.sys.locale", languageTag);
  34. SystemProperties.set("persist.sys.language", "");
  35. SystemProperties.set("persist.sys.country", "");
  36. SystemProperties.set("persist.sys.localevar", "");
  37. }
  38. // The system server should never make non-oneway calls
  39. Binder.setWarnOnBlocking(true);
  40. // The system server should always load safe labels
  41. PackageItemInfo.setForceSafeLabels(true);
  42. // Deactivate SQLiteCompatibilityWalFlags until settings provider is initialized
  43. SQLiteCompatibilityWalFlags.init(null);
  44. // Here we go!
  45. Slog.i(TAG, "Entered the Android system server!");
  46. int uptimeMillis = (int) SystemClock.elapsedRealtime();
  47. EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, uptimeMillis);
  48. if (!mRuntimeRestart) {
  49. MetricsLogger.histogram(null, "boot_system_server_init", uptimeMillis);
  50. }
  51. // In case the runtime switched since last boot (such as when
  52. // the old runtime was removed in an OTA), set the system
  53. // property so that it is in sync. We can | xq oqi't do this in
  54. // libnativehelper's JniInvocation::Init code where we already
  55. // had to fallback to a different runtime because it is
  56. // running as root and we need to be the system user to set
  57. // the property. http://b/11463182
  58. SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());
  59. // Mmmmmm... more memory!
  60. VMRuntime.getRuntime().clearGrowthLimit();
  61. // The system server has to run all of the time, so it needs to be
  62. // as efficient as possible with its memory usage.
  63. VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
  64. // Some devices rely on runtime fingerprint generation, so make sure
  65. // we've defined it before booting further.
  66. Build.ensureFingerprintProperty();
  67. // Within the system server, it is an error to access Environment paths without
  68. // explicitly specifying a user.
  69. Environment.setUserRequired(true);
  70. // Within the system server, any incoming Bundles should be defused
  71. // to avoid throwing BadParcelableException.
  72. BaseBundle.setShouldDefuse(true);
  73. // Within the system server, when parceling exceptions, include the stack trace
  74. Parcel.setStackTraceParceling(true);
  75. // Ensure binder calls into the system always run at foreground priority.
  76. BinderInternal.disableBackgroundScheduling(true);
  77. // Increase the number of binder threads in system_server
  78. BinderInternal.setMaxThreads(sMaxBinderThreads);
  79. // Prepare the main looper thread (this thread).
  80. android.os.Process.setThreadPriority(
  81. android.os.Process.THREAD_PRIORITY_FOREGROUND);
  82. android.os.Process.setCanSelfBackground(false);
  83. Looper.prepareMainLooper();
  84. Looper.getMainLooper().setSlowLogThresholdMs(
  85. SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
  86. // Initialize native services.
  87. System.loadLibrary("android_servers");
  88. // Check whether we failed to shut down last time we tried.
  89. // This call may not return.
  90. performPendingShutdown();
  91. // Initialize the system context.
  92. createSystemContext();
  93. // Create the system service manager.
  94. mSystemServiceManager = new SystemServiceManager(mSystemContext);
  95. mSystemServiceManager.setStartInfo(mRuntimeRestart,
  96. mRuntimeStartElapsedTime, mRuntimeStartUptime);
  97. LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
  98. // Prepare the thread pool for init tasks that can be parallelized
  99. SystemServerInitThreadPool.get();
  100. } finally {
  101. traceEnd(); // InitBeforeStartServices
  102. }
  103. // Start services.
  104. try {
  105. traceBeginAndSlog("StartServices");
  106. startBootstrapServices();
  107. startCoreServices();
  108. startOtherServices();
  109. SystemServerInitThreadPool.shutdown();
  110. } catch (Throwable ex) {
  111. Slog.e("System", "******************************************");
  112. Slog.e("System", "************ Failure starting system services", ex);
  113. throw ex;
  114. } finally {
  115. traceEnd();
  116. }
  117. StrictMode.initVmDefaults(null);
  118. if (!mRuntimeRestart && !isFirstBootOrUpgrade()) {
  119. int uptimeMillis = (int) SystemClock.elapsedRealtime();
  120. MetricsLogger.histogram(null, "boot_system_server_ready", uptimeMillis);
  121. final int MAX_UPTIME_MILLIS = 60 * 1000;
  122. if (uptimeMillis > MAX_UPTIME_MILLIS) {
  123. Slog.wtf(SYSTEM_SERVER_TIMING_TAG,
  124. "SystemServer init took too long. uptimeMillis=" + uptimeMillis);
  125. }
  126. }
  127. // Loop forever.
  128. Looper.loop();
  129. throw new RuntimeException("Main thread loop unexpectedly exited");
  130. }

看看main方法的主要关键部分吧设置完进程属性后直接创建了Looper然后初始化本地service,还发现了createSystemContext();我们进去看看到底做了什么:

  1. private void createSystemContext() {
  2. ActivityThread activityThread = ActivityThread.systemMain();
  3. mSystemContext = activityThread.getSystemContext();
  4. mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
  5. final Context systemUiContext = activityThread.getSystemUiContext();
  6. systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
  7. }

这直接调了ActivityThread的systemMain方法:

  1. public static ActivityThread systemMain() {
  2. // The system process on low-memory devices do not get to use hardware
  3. // accelerated drawing, since this can add too much overhead to the
  4. // process.
  5. if (!ActivityManager.isHighEndGfx()) {
  6. ThreadedRenderer.disable(true);
  7. } else {
  8. ThreadedRenderer.enableForegroundTrimming();
  9. }
  10. ActivityThread thread = new ActivityThread();
  11. thread.attach(true, 0);
  12. return thread;
  13. }

看thread.attach(true, 0);这不是ActivityThread的main方法调的吗看样子这不就是走创建Application流程吗,直接创建应用执行生命周期的吗,而且我们还看到activityThread.getSystemContext():

  1. public ContextImpl getSystemContext() {
  2. synchronized (this) {
  3. if (mSystemContext == null) {
  4. mSystemContext = ContextImpl.createSystemContext(this);
  5. }
  6. return mSystemContext;
  7. }
  8. }

这不就是Activity的Context吗,那好我们再回去看看SystemServer到底要用Context干嘛接下来mSystemServiceManager = new SystemServiceManager(mSystemContext);后续都是围绕开启service最好循环Looper;我们都知道Binder通信都是借助service绑定context生命周期的所以如果zygote进程要是有Binder通信的户也需要像system_server进程这样createSystemContext()以及创建不启动service,那么我们回想一下zygote的main方法想想zygote进程到底是干嘛的,zygote进程主要职责不就是fork子进程的吗,如果再创建systemContext创建service这么一通只为了实现Binder通信大家想想这么做是不是有点太累赘了。有人估计会问那你system_server进程创建这些就不是累赘了吗,当然不是累赘了,首先system_server进程职责就是管理Android系统的所有service服务的,再者担任Luncher桌面可视化系统以及安装APK应用职责责任重大。所以zygote进程与system_server进程使用socket通信就代码简单多了没有那么繁杂累赘是最合适的选择方式。

init进程是Linux系统开机最先启动基础进程之一的用户进程,Linux Kernel启动后,会调用/system/core/init/Init.cpp的main()方法,可决定用户级别,Android系统其它进程都是由init进程创建和控制的;

zygote进程是init进程fork出的第一个子进程,由init进程的service通过socket通信开启进程,然后zygote进程调用startVm函数创建虚拟机实例注册JNI开始与Java通信,通过JNI调com.android.internal.os.ZygoteInit的main函数forkSystemServer开始fork出system_server进程设置Dalvik(ART)虚拟机,主要用来与Dalvik(ART)虚拟机通信,所以Dalvik(ART)虚拟机都是有zygote进程孵化器fork出来的,所谓fork其实就是复制zygote进程,这样创建速度快且能够共享系统和框架的资源。system_server进程是zygote进程通过startSystemServer方法fork出的第一个子进程,主要用于启动Android系统中的服务,常见的比如WindowManagerServer(WMS)、ActivityManagerSystemService(AMS)、 PackageManagerServer(PMS)等,ActivityManagerSystemService管理所有Activity,WindowManagerService管理所有窗口,PackageManagerServer主要是安装apk是用来解析AndroidManifest.xml获取Activity、Service、Broadcast Receiver、Content Provider以及权限信息存储Binder中,并以友好的方式在桌面上展现出来(快捷图标的形式)。这里为什么提到了Launcher桌面,因为system_server进程启动Android系统中的服务同时启动了Launcher应用程序,想看Launcher启动流程源码请到我的Android系统应用Launcher的启动流程结合源码详解

Application进程就是应用进程也就是我们安装的应用,点击桌面luncher图标,通过ActivityManagerSystemService通知system_server进程LocalSocket连接zygote进程serverSocket传递ActivityThread参数,然后system_server进程fork出当前Application应用进程并调度ActivityThread的main()函数启动应用程序,当然这个流程也就是App的启动流程。

总结:所有Binder通信都需要有context、service、aidl三者缺一不可,所以别人再问你为什么使用Socket不有Binder你就可以参考这个Binder通信需要context绑定service然后在service创建aidl接口实现类获取Binder对象来实现通信,如果被问的进程不需要context和service就不需要使用Binder通信直接使用socket通信更简洁。

欢迎关注微信公众号!你的每个赞和在看,都是对我的支持!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/227805
推荐阅读
相关标签