{"id":13900,"date":"2024-07-03T15:25:16","date_gmt":"2024-07-03T07:25:16","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900"},"modified":"2024-09-29T15:13:54","modified_gmt":"2024-09-29T07:13:54","slug":"system_server%e8%bf%9b%e7%a8%8b%e5%90%af%e5%8a%a8%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900","title":{"rendered":"system_server\u8fdb\u7a0b\u542f\u52a8\u539f\u7406"},"content":{"rendered":"<p><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/343233982\">https:\/\/zhuanlan.zhihu.com\/p\/343233982<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/xingyu19911016\/article\/details\/128619982\">https:\/\/blog.csdn.net\/xingyu19911016\/article\/details\/128619982<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/qq_21154101\/article\/details\/89455884\">https:\/\/blog.csdn.net\/qq_21154101\/article\/details\/89455884<\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#%E4%B8%80%E3%80%81%E5%88%9D%E5%A7%8B%E5%8C%96%EF%BC%88InitBeforeStartServices%EF%BC%89\" title=\"\u4e00\u3001\u521d\u59cb\u5316\uff08InitBeforeStartServices\uff09\">\u4e00\u3001\u521d\u59cb\u5316\uff08InitBeforeStartServices\uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#%E4%BA%8C%E3%80%81%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1\" title=\"\u4e8c\u3001\u542f\u52a8\u670d\u52a1\">\u4e8c\u3001\u542f\u52a8\u670d\u52a1<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#1%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%A4%9A%E7%A7%8D%E5%88%9B%E5%BB%BA%E5%90%AF%E5%8A%A8%E6%96%B9%E5%BC%8F\" title=\"1.\u670d\u52a1\u7684\u591a\u79cd\u521b\u5efa\u542f\u52a8\u65b9\u5f0f\">1.\u670d\u52a1\u7684\u591a\u79cd\u521b\u5efa\u542f\u52a8\u65b9\u5f0f<\/a><ul class=\"ez-toc-list-level-4\"><li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E9%80%9A%E8%BF%87SystemServiceManagerjava%E7%9A%84startService%E6%96%B9%E6%B3%95\" title=\"\u65b9\u5f0f\u4e00\uff1a\u901a\u8fc7SystemServiceManager.java\u7684startService\u65b9\u6cd5\">\u65b9\u5f0f\u4e00\uff1a\u901a\u8fc7SystemServiceManager.java\u7684startService\u65b9\u6cd5<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%B8%B8%E8%A7%84new%E6%9C%8D%E5%8A%A1%E7%B1%BB\" title=\"\u65b9\u5f0f\u4e8c\uff1a\u5e38\u89c4new\u670d\u52a1\u7c7b\">\u65b9\u5f0f\u4e8c\uff1a\u5e38\u89c4new\u670d\u52a1\u7c7b<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#2%E7%BC%93%E5%AD%98%E6%9C%8D%E5%8A%A1\" title=\"2.\u7f13\u5b58\u670d\u52a1\">2.\u7f13\u5b58\u670d\u52a1<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#3%E5%BC%80%E6%9C%BA%E5%90%AF%E5%8A%A8%E6%89%A7%E8%A1%8C%E5%90%84%E4%B8%AA%E9%98%B6%E6%AE%B5%E7%9A%84%E5%9B%9E%E8%B0%83\" title=\"3.\u5f00\u673a\u542f\u52a8\u6267\u884c\u5404\u4e2a\u9636\u6bb5\u7684\u56de\u8c03\">3.\u5f00\u673a\u542f\u52a8\u6267\u884c\u5404\u4e2a\u9636\u6bb5\u7684\u56de\u8c03<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#3%E5%85%B7%E4%BD%93%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90\" title=\"3.\u5177\u4f53\u6e90\u7801\u5206\u6790\">3.\u5177\u4f53\u6e90\u7801\u5206\u6790<\/a><ul class=\"ez-toc-list-level-4\"><li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13900\/#3-1startBootstrapServices\" title=\"3-1)startBootstrapServices\">3-1)startBootstrapServices<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%E4%B8%80%E3%80%81%E5%88%9D%E5%A7%8B%E5%8C%96%EF%BC%88InitBeforeStartServices%EF%BC%89\"><\/span>\u4e00\u3001\u521d\u59cb\u5316\uff08InitBeforeStartServices\uff09<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>\n<p>\u51c6\u5907MainLooper\u7ebf\u7a0b<\/p>\n<pre><code class=\"language-java\">\/\/ Prepare the main looper thread (this thread).\nandroid.os.Process.setThreadPriority(\n    android.os.Process.THREAD_PRIORITY_FOREGROUND);\nandroid.os.Process.setCanSelfBackground(false);\nLooper.prepareMainLooper();\nLooper.getMainLooper().setSlowLogThresholdMs(\n    SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);<\/code><\/pre>\n<\/li>\n<li>\n<p>\u52a0\u8f7dnative\u5e93<\/p>\n<pre><code class=\"language-java\">\/\/ Initialize native services.\nSystem.loadLibrary(&quot;android_servers&quot;);<\/code><\/pre>\n<\/li>\n<li>\n<p>createSystemContext<\/p>\n<pre><code class=\"language-java\">613     private void createSystemContext() {\n614         ActivityThread activityThread = ActivityThread.systemMain();\n615         mSystemContext = activityThread.getSystemContext();\n616         mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);\n617 \n618         final Context systemUiContext = activityThread.getSystemUiContext();\n619         systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);\n620     }<\/code><\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efaSystemServiceManager\u5b9e\u4f8b<\/p>\n<pre><code class=\"language-java\">501             \/\/ Create the system service manager.\n502             mSystemServiceManager = new SystemServiceManager(mSystemContext);\n503             mSystemServiceManager.setStartInfo(mRuntimeRestart,\n504                     mRuntimeStartElapsedTime, mRuntimeStartUptime);\n505             LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);\n506             \/\/ Prepare the thread pool for init tasks that can be parallelized<\/code><\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa\u7ebf\u7a0b\u6c60<br \/>\n<code> 507             SystemServerInitThreadPool.get();<\/code><\/p>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E3%80%81%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1\"><\/span>\u4e8c\u3001\u542f\u52a8\u670d\u52a1<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-java\"> 512         \/\/ Start services.\n 513         try {\n 514             traceBeginAndSlog(&quot;StartServices&quot;);\n 515             startBootstrapServices();\n 516             startCoreServices();\n 517             startOtherServices();\n 518             SystemServerInitThreadPool.shutdown();\n 519         } catch (Throwable ex) {\n 520             Slog.e(&quot;System&quot;, &quot;******************************************&quot;);\n 521             Slog.e(&quot;System&quot;, &quot;************ Failure starting system services&quot;, ex);\n 522             throw ex;\n 523         } finally {\n 524             traceEnd();\n 525         }<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"1%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%A4%9A%E7%A7%8D%E5%88%9B%E5%BB%BA%E5%90%AF%E5%8A%A8%E6%96%B9%E5%BC%8F\"><\/span>1.\u670d\u52a1\u7684\u591a\u79cd\u521b\u5efa\u542f\u52a8\u65b9\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E9%80%9A%E8%BF%87SystemServiceManagerjava%E7%9A%84startService%E6%96%B9%E6%B3%95\"><\/span>\u65b9\u5f0f\u4e00\uff1a\u901a\u8fc7SystemServiceManager.java\u7684startService\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5927\u591a\u6570\u7684\u7cfb\u7edf\u670d\u52a1\u90fd\u662f\u901a\u8fc7\u6b64\u65b9\u6cd5\u542f\u52a8\u7684<\/p>\n<p>SystemServiceManager.java\u6e90\u7801\u4f4d\u7f6e\uff1a<code>frameworks\/base\/services\/core\/java\/com\/android\/server\/SystemServiceManager.java<\/code><\/p>\n<p>startService\u67093\u4e2a\u91cd\u8f7d\u65b9\u6cd5\uff0c\u6700\u7ec8\u8c03\u7528\u65b9\u6cd5\u4e09\uff0c\u6240\u6709\u7684\u670d\u52a1\u90fd\u662fSystemService\u7684\u5b50\u7c7b\u3002<\/p>\n<pre><code class=\"language-java\">\/\/\u65b9\u6cd5\u4e00\npublic SystemService startService(String className)\n\/\/\u65b9\u6cd5\u4e8c\npublic &lt;T extends SystemService&gt; T startService(Class&lt;T&gt; serviceClass)\n\n\/\/\u65b9\u6cd5\u4e09\n124     public void startService(@NonNull final SystemService service) {\n125         \/\/ Register it.\n            \/\/mServices\u662f\u4e00\u4e2aArrayList\n126         mServices.add(service);\n127         \/\/ Start it.\n128         long time = SystemClock.elapsedRealtime();\n129         try {\n130             service.onStart();\n131         } catch (RuntimeException ex) {\n132             throw new RuntimeException(&quot;Failed to start service &quot; + service.getClass().getName()\n133                     + &quot;: onStart threw an exception&quot;, ex);\n134         }\n135         warnIfTooLong(SystemClock.elapsedRealtime() - time, service, &quot;onStart&quot;);\n136     }<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%B8%B8%E8%A7%84new%E6%9C%8D%E5%8A%A1%E7%B1%BB\"><\/span>\u65b9\u5f0f\u4e8c\uff1a\u5e38\u89c4new\u670d\u52a1\u7c7b<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li>\u9759\u6001main\u65b9\u6cd5<br \/>\nPackageManagerService\u3001OtaDexoptService\u3001WindowManagerService<\/li>\n<li>\u9759\u6001create\u65b9\u6cd5<br \/>\nNetworkManagementService\u3001IpSecService\u3001NsdService\u3001NetworkStatsService<\/li>\n<li>\u76f4\u63a5New<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"2%E7%BC%93%E5%AD%98%E6%9C%8D%E5%8A%A1\"><\/span>2.\u7f13\u5b58\u670d\u52a1<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>LocalServices.addService<br \/>\n\u5c06\u670d\u52a1\u7f13\u5b58\u8d77\u6765\uff0c\u4f9bsystem_server\u8fdb\u7a0b\u4e2d\u7684\u5176\u5b83\u670d\u52a1\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p>SystemServer\u4f1a\u8c03\u7528\u6b64\u65b9\u6cd5\uff0c\u5176\u5b83\u7684\u670d\u52a1\u7684\u6784\u9020\u65b9\u6cd5\u4e00\u822c\u90fd\u4f1a\u8c03\u7528\u6b64\u65b9\u6cd5\u3002<\/p>\n<ul>\n<li>ServiceManager.addService<br \/>\nServiceManager.java\u7c7b\u89c1<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14112\">http:\/\/xinyiworld.top\/wordpress_it\/?p=14112<\/a><\/li>\n<\/ul>\n<p>\u5c06\u670d\u52a1\u7684Binder\u7f13\u5b58\u8d77\u6765\uff0c\u4f9b\u5176\u5b83\u8fdb\u7a0b\uff08\u4e00\u822c\u662f\u5e94\u7528\u8fdb\u7a0b\uff09\u8de8\u8fdb\u7a0b\u8c03\u7528\u3002<br \/>\n\u7edd\u5927\u591a\u6570\u7684\u670d\u52a1\u90fd\u662f\u5728SystemServer\u7c7b\u4e2d\u901a\u8fc7ServiceManager.addService\u6ce8\u518c\uff0c\u4e5f\u6709\u90e8\u5206\u90e8\u5206\u662f\u5728\u672c\u7c7b\u4e2d\u8c03\u7528ServiceManager.addService\u6ce8\u518c\u7684\uff0c\u5982ActivityManagerService\u4e0ePackageManagerService<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3%E5%BC%80%E6%9C%BA%E5%90%AF%E5%8A%A8%E6%89%A7%E8%A1%8C%E5%90%84%E4%B8%AA%E9%98%B6%E6%AE%B5%E7%9A%84%E5%9B%9E%E8%B0%83\"><\/span>3.\u5f00\u673a\u542f\u52a8\u6267\u884c\u5404\u4e2a\u9636\u6bb5\u7684\u56de\u8c03<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-java\">138     \/**\n139      * Starts the specified boot phase for all system services that have been started up to\n140      * this point.\n141      *\n142      * @param phase The boot phase to start.\n143      *\/\n144     public void startBootPhase(final int phase) {\n145         if (phase &lt;= mCurrentPhase) {\n146             throw new IllegalArgumentException(&quot;Next phase must be larger than previous&quot;);\n147         }\n148         mCurrentPhase = phase;\n149 \n150         Slog.i(TAG, &quot;Starting phase &quot; + mCurrentPhase);\n151         try {\n152             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, &quot;OnBootPhase &quot; + phase);\n153             final int serviceLen = mServices.size();\n154             for (int i = 0; i &lt; serviceLen; i++) {\n155                 final SystemService service = mServices.get(i);\n156                 long time = SystemClock.elapsedRealtime();\n157                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, service.getClass().getName());\n158                 try {\n159                     service.onBootPhase(mCurrentPhase);\n160                 } catch (Exception ex) {\n161                     throw new RuntimeException(&quot;Failed to boot service &quot;\n162                             + service.getClass().getName()\n163                             + &quot;: onBootPhase threw an exception during phase &quot;\n164                             + mCurrentPhase, ex);\n165                 }\n166                 warnIfTooLong(SystemClock.elapsedRealtime() - time, service, &quot;onBootPhase&quot;);\n167                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);\n168             }\n169         } finally {\n170             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);\n171         }\n172     }<\/code><\/pre>\n<p>\u5728SystemServiceManager.java\u7684startService\u65b9\u6cd5\u4e2dmServices\u4f1a\u5c06\u5404\u4e2aSystemService\u7f13\u5b58\u8d77\u6765\uff0c\u6b64\u5904\u518d\u904d\u5386\u6267\u884c\u5404\u4e2aSystemService\u7684onBootPhase\u65b9\u6cd5\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3%E5%85%B7%E4%BD%93%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90\"><\/span>3.\u5177\u4f53\u6e90\u7801\u5206\u6790<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"3-1startBootstrapServices\"><\/span>3-1)startBootstrapServices<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li>\n<p>\u542f\u52a8WatchDog\u5b50\u7ebf\u7a0b\uff0c\u76d1\u542c\u7cfb\u7edf\u670d\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p>Reading configuration<br \/>\n<code> SystemServerInitThreadPool.get().submit(SystemConfig::getInstance, TAG_SYSTEM_CONFIG);<\/code><br \/>\n\u8fd9\u4e2a\u4ee3\u7801\u6309\u9053\u7406\u8bf4\u4f1a\u62a5\u9519\uff0cSystemConfig\u90fd\u6ca1\u6709\u5b9e\u73b0runnable\u63a5\u53e3\uff0c\u600e\u4e48\u80fd\u52a0\u5165\u7ebf\u7a0b\u6c60\u6267\u884c\uff1f<\/p>\n<\/li>\n<\/ul>\n<button class=\"simplefavorite-button\" data-postid=\"13900\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>https:\/\/zhuanlan.zhihu.com\/p\/343233982 https:\/\/blo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1059],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13900"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13900"}],"version-history":[{"count":28,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13900\/revisions"}],"predecessor-version":[{"id":14817,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13900\/revisions\/14817"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13900"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}