news 2026/8/16 13:55:01

Android 12 S SystemServer启动过程中的服务分析

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Android 12 S SystemServer启动过程中的服务分析

SystemServer是由zygote启动的,启动后会先走main方法,看注释也可以看到此方法是"The main entry point from zygote."

接着往下走,就到了run方法

frameworks/base/services/java/com/android/server/SystemServer.java

public static void main(String[] args) { new SystemServer().run(); } private void run() { ... Looper.prepareMainLooper(); Looper.getMainLooper().setSlowLogThresholdMs( SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS); // Start services. try { t.traceBegin("StartServices"); startBootstrapServices(t); startCoreServices(t); startOtherServices(t); } catch (Throwable ex) { Slog.e("System", "******************************************"); Slog.e("System", "************ Failure starting system services", ex); throw ex; } finally { t.traceEnd(); // StartServices } ... // Loop forever. Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }

然后走startBootstrapServices,这里主要是启动一些系统的关键服务,这些服务具有复杂的相互依赖关系。

/** * Starts the small tangle of critical services that are needed to get the system off the * ground. These services have complex mutual dependencies which is why we initialize them all * in one place here. Unless your service is also entwined in these dependencies, it should be * initialized in one of the other functions. */ private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) { t.traceBegin("startBootstrapServices"); ... // Activity manager runs the show. t.traceBegin("StartActivityManager"); // TODO: Might need to move after migration to WM. ActivityTaskManagerService atm = mSystemServiceManager.startService( ActivityTaskManagerService.Lifecycle.class).getService(); mActivityManagerService = ActivityManagerService.Lifecycle.startService( mSystemServiceManager, atm); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); mWindowManagerGlobalLock = atm.getGlobalLock(); t.traceEnd(); ... // Set up the Application instance for the system process and get started. t.traceBegin("SetSystemProcess"); mActivityManagerService.setSystemProcess(); t.traceEnd(); ... t.traceBegin("StartPackageManagerService"); try { Watchdog.getInstance().pauseWatchingCurrentThread("packagemanagermain"); mPackageManagerService = PackageManagerService.main(mSystemContext, installer, domainVerificationService, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); } finally { Watchdog.getInstance().resumeWatchingCurrentThread("packagemanagermain"); } }

然后走startCoreServices方法,这个方法主要是启动一些在开机过程中没有互相依赖的基本服务。

/** * Starts some essential services that are not tangled up in the bootstrap process. */ private void startCoreServices(@NonNull TimingsTraceAndSlog t) { t.traceBegin("startCoreServices"); // Service for system config t.traceBegin("StartSystemConfigService"); mSystemServiceManager.startService(SystemConfigService.class); t.traceEnd(); t.traceBegin("StartBatteryService"); // Tracks the battery level. Requires LightService. mSystemServiceManager.startService(BatteryService.class); t.traceEnd(); ... // Service for GPU and GPU driver. t.traceBegin("GpuService"); mSystemServiceManager.startService(GpuService.class); t.traceEnd(); // Handles system process requests for remotely provisioned keys & data. t.traceBegin("StartRemoteProvisioningService"); mSystemServiceManager.startService(RemoteProvisioningService.class); t.traceEnd(); // TODO(b/277600174): Start CpuMonitorService on all builds and not just on debuggable // builds once the Android JobScheduler starts using this service. if (Build.IS_DEBUGGABLE || Build.IS_ENG) { // Service for CPU monitor. t.traceBegin("CpuMonitorService"); mSystemServiceManager.startService(CpuMonitorService.class); t.traceEnd(); } t.traceEnd(); // startCoreServices }

然后走startOtherServices方法,此方法中主要是启动一些系统中其他的服务

/** * Starts a miscellaneous grab bag of stuff that has yet to be refactored and organized. */ private void startOtherServices(@NonNull TimingsTraceAndSlog t) { t.traceBegin("startOtherServices"); final Context context = mSystemContext; DynamicSystemService dynamicSystem = null; IStorageManager storageManager = null; NetworkManagementService networkManagement = null; IpSecService ipSecService = null; VpnManagerService vpnManager = null; VcnManagementService vcnManagement = null; NetworkStatsService networkStats = null; NetworkPolicyManagerService networkPolicy = null; NsdService serviceDiscovery = null; WindowManagerService wm = null; SerialService serial = null; NetworkTimeUpdateService networkTimeUpdater = null; InputManagerService inputManager = null; TelephonyRegistry telephonyRegistry = null; ConsumerIrService consumerIr = null; MmsServiceBroker mmsService = null; HardwarePropertiesManagerService hardwarePropertiesService = null; PacProxyService pacProxyService = null; ... t.traceBegin("StartVibratorManagerService"); mSystemServiceManager.startService(VibratorManagerService.Lifecycle.class); t.traceEnd(); t.traceBegin("StartDynamicSystemService"); dynamicSystem = new DynamicSystemService(context); ServiceManager.addService("dynamic_system", dynamicSystem); t.traceEnd(); if (!isWatch) { t.traceBegin("StartConsumerIrService"); consumerIr = new ConsumerIrService(context); ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr); t.traceEnd(); } t.traceBegin("StartAlarmManagerService"); mSystemServiceManager.startService(ALARM_MANAGER_SERVICE_CLASS); t.traceEnd(); t.traceBegin("StartInputManagerService"); inputManager = new InputManagerService(context); t.traceEnd(); ... }

以下是一个在systemserver中启动AMS的流程

且WindowManagerService以及ActivityManagerService等的服务的进程号都是SystemServer,但是是跑在不同的线程上

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/16 13:49:57

秋冬鞋履旺季重磅商标+外观专利双轨TRO突袭!UGG品牌母公司Deckers Outdoor Corporation委托行业头部GBC律所发起大规模批量密封诉讼,26-cv-9356雪地靴、居家拖鞋、

一、案件基础档案📋 案号:26-cv-9356 原告品牌:UGG 雪地靴 品牌方:Deckers Outdoor Corporation 起诉地:美国伊利诺伊州 代理律所:GBC 起诉时间:2026年08月05日 维权案由:商标…

作者头像 李华
网站建设 2026/8/16 13:44:27

Shopify自动化运营实战:Codex工具部署、API集成与AI赋能电商

这次我们来看一个 Shopify 建站与自动化运营的实战项目。一位拥有 20 年经验的卖家,免费分享了一套基于 Codex 工具实现 Shopify 独立站自动化运营的完整流程。对于想要入局跨境电商、提升店铺运营效率,或者对 AI 工具如何赋能电商业务感兴趣的读者来说&…

作者头像 李华
网站建设 2026/8/16 13:41:31

如何零基础接入直播弹幕抓取?BarrageGrab 直连方案上手完整指南

如何零基础接入直播弹幕抓取?BarrageGrab 直连方案上手完整指南 【免费下载链接】BarrageGrab 抖音快手bilibili直播弹幕wss直连,非系统代理方式,无需多开浏览器窗口 项目地址: https://gitcode.com/gh_mirrors/ba/BarrageGrab 如果你…

作者头像 李华
网站建设 2026/8/16 13:30:32

选智能呼叫系统,别再被SaaS“按月付费”割韭菜了

帮朋友看呼叫中心系统的时候,发现市面上SaaS产品几乎清一色按坐席按月收费。一个10坐席的配置,年费轻松过万。小团队咬牙付了,第二年续费又是一笔。直到刷到PCSwitch(www.pcswitch.cn),基础版500元/套&…

作者头像 李华