Android 系统服务大管家 SystemServer:从 fork 到 AMS/WMS/PMS,60+ 服务是怎么一个一个拉起来的?(附自定义系统服务实战)
目录
- [一、SystemServer 是什么](#一、SystemServer 是什么)
- [二、SystemServer 是怎样被 fork 出来的](#二、SystemServer 是怎样被 fork 出来的)
- [三、SystemServer 的启动流程总览](#三、SystemServer 的启动流程总览)
- [四、启动阶段一:Bootstrap 服务](#四、启动阶段一:Bootstrap 服务)
- [五、启动阶段二:Core 服务](#五、启动阶段二:Core 服务)
- [六、启动阶段三:Other 服务](#六、启动阶段三:Other 服务)
- 七、系统服务的依赖管理
- [八、Binder 注册:ServiceManager](#八、Binder 注册:ServiceManager)
- 九、实战:添加一个自定义系统服务
- 十、实战:看看你手机里跑了多少系统服务
- 十一、常见踩坑记录
- 十二、总结
一、SystemServer 是什么
上一篇文章讲了 Zygote 是 Android 所有 App 进程的爹。但在 fork App 之前,Zygote 先 fork 了自己的第一个孩子------SystemServer。
SystemServer 是 Android 框架层的大本营。你平时听到的 AMS(ActivityManagerService)、WMS(WindowManagerService)、PMS(PackageManagerService)、PMS 之外的 PMS(PowerManagerService)、InputManagerService......全部跑在 SystemServer 这个进程里。
#mermaid-svg-MGg5dC8TnybkKyiP{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-MGg5dC8TnybkKyiP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MGg5dC8TnybkKyiP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MGg5dC8TnybkKyiP .error-icon{fill:#552222;}#mermaid-svg-MGg5dC8TnybkKyiP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MGg5dC8TnybkKyiP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MGg5dC8TnybkKyiP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MGg5dC8TnybkKyiP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MGg5dC8TnybkKyiP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MGg5dC8TnybkKyiP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MGg5dC8TnybkKyiP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MGg5dC8TnybkKyiP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MGg5dC8TnybkKyiP .marker.cross{stroke:#333333;}#mermaid-svg-MGg5dC8TnybkKyiP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MGg5dC8TnybkKyiP p{margin:0;}#mermaid-svg-MGg5dC8TnybkKyiP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-MGg5dC8TnybkKyiP .cluster-label text{fill:#333;}#mermaid-svg-MGg5dC8TnybkKyiP .cluster-label span{color:#333;}#mermaid-svg-MGg5dC8TnybkKyiP .cluster-label span p{background-color:transparent;}#mermaid-svg-MGg5dC8TnybkKyiP .label text,#mermaid-svg-MGg5dC8TnybkKyiP span{fill:#333;color:#333;}#mermaid-svg-MGg5dC8TnybkKyiP .node rect,#mermaid-svg-MGg5dC8TnybkKyiP .node circle,#mermaid-svg-MGg5dC8TnybkKyiP .node ellipse,#mermaid-svg-MGg5dC8TnybkKyiP .node polygon,#mermaid-svg-MGg5dC8TnybkKyiP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MGg5dC8TnybkKyiP .rough-node .label text,#mermaid-svg-MGg5dC8TnybkKyiP .node .label text,#mermaid-svg-MGg5dC8TnybkKyiP .image-shape .label,#mermaid-svg-MGg5dC8TnybkKyiP .icon-shape .label{text-anchor:middle;}#mermaid-svg-MGg5dC8TnybkKyiP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-MGg5dC8TnybkKyiP .rough-node .label,#mermaid-svg-MGg5dC8TnybkKyiP .node .label,#mermaid-svg-MGg5dC8TnybkKyiP .image-shape .label,#mermaid-svg-MGg5dC8TnybkKyiP .icon-shape .label{text-align:center;}#mermaid-svg-MGg5dC8TnybkKyiP .node.clickable{cursor:pointer;}#mermaid-svg-MGg5dC8TnybkKyiP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-MGg5dC8TnybkKyiP .arrowheadPath{fill:#333333;}#mermaid-svg-MGg5dC8TnybkKyiP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MGg5dC8TnybkKyiP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MGg5dC8TnybkKyiP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MGg5dC8TnybkKyiP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-MGg5dC8TnybkKyiP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MGg5dC8TnybkKyiP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-MGg5dC8TnybkKyiP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MGg5dC8TnybkKyiP .cluster text{fill:#333;}#mermaid-svg-MGg5dC8TnybkKyiP .cluster span{color:#333;}#mermaid-svg-MGg5dC8TnybkKyiP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MGg5dC8TnybkKyiP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-MGg5dC8TnybkKyiP rect.text{fill:none;stroke-width:0;}#mermaid-svg-MGg5dC8TnybkKyiP .icon-shape,#mermaid-svg-MGg5dC8TnybkKyiP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MGg5dC8TnybkKyiP .icon-shape p,#mermaid-svg-MGg5dC8TnybkKyiP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-MGg5dC8TnybkKyiP .icon-shape .label rect,#mermaid-svg-MGg5dC8TnybkKyiP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MGg5dC8TnybkKyiP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-MGg5dC8TnybkKyiP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-MGg5dC8TnybkKyiP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} SystemServer 进程
AMS
ActivityManagerService
管理四大组件生命周期
WMS
WindowManagerService
管理窗口和触摸事件
PMS
PackageManagerService
管理 APK 安装/权限
InputManagerService
鼠标/键盘/触控分发
PowerManagerService
电源和唤醒锁
DisplayManagerService
多屏管理
... 60+ 个系统服务
一句话:你手机上能正常运行的所有系统功能------打开 App、显示窗口、分发触摸事件、管理电量------背后都是 SystemServer 里某个 Service 在干活。
二、SystemServer 是怎样被 fork 出来的
回忆一下 ZygoteInit 的逻辑:
java
// ZygoteInit.main() 简化
public static void main(String[] argv) {
preload(); // 1. 预加载
if (startSystemServer) {
forkSystemServer(...); // 2. ★ fork SystemServer
}
runSelectLoop(); // 3. 等 AMS 发 fork App 的命令
}
forkSystemServer 做了什么:
java
private static Runnable forkSystemServer(String abiList, String socketName,
ZygoteServer zygoteServer) {
// 构造启动参数
String[] args = {
"--setuid=1000", // system 用户
"--setgid=1000",
"--nice-name=system_server",
"--capabilities=...",
"com.android.server.SystemServer", // ★ 入口类
};
// ★ 调用 Zygote.forkAndSpecialize ------ fork + 设好 UID/GID
int pid = Zygote.forkAndSpecialize(parsedArgs);
if (pid == 0) {
// 子进程(SystemServer)
zygoteServer.closeServerSocket(); // 关闭 Zygote 的 Socket
return handleSystemServerProcess(parsedArgs); // 返回 Runnable
}
// 父进程(Zygote):啥也不干,继续跑后面的 runSelectLoop
return null;
}
时序关系:
#mermaid-svg-fO2WGJvqDUwJk42C{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-fO2WGJvqDUwJk42C .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-fO2WGJvqDUwJk42C .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-fO2WGJvqDUwJk42C .error-icon{fill:#552222;}#mermaid-svg-fO2WGJvqDUwJk42C .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-fO2WGJvqDUwJk42C .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-fO2WGJvqDUwJk42C .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-fO2WGJvqDUwJk42C .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-fO2WGJvqDUwJk42C .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-fO2WGJvqDUwJk42C .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-fO2WGJvqDUwJk42C .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-fO2WGJvqDUwJk42C .marker{fill:#333333;stroke:#333333;}#mermaid-svg-fO2WGJvqDUwJk42C .marker.cross{stroke:#333333;}#mermaid-svg-fO2WGJvqDUwJk42C svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-fO2WGJvqDUwJk42C p{margin:0;}#mermaid-svg-fO2WGJvqDUwJk42C .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-fO2WGJvqDUwJk42C .cluster-label text{fill:#333;}#mermaid-svg-fO2WGJvqDUwJk42C .cluster-label span{color:#333;}#mermaid-svg-fO2WGJvqDUwJk42C .cluster-label span p{background-color:transparent;}#mermaid-svg-fO2WGJvqDUwJk42C .label text,#mermaid-svg-fO2WGJvqDUwJk42C span{fill:#333;color:#333;}#mermaid-svg-fO2WGJvqDUwJk42C .node rect,#mermaid-svg-fO2WGJvqDUwJk42C .node circle,#mermaid-svg-fO2WGJvqDUwJk42C .node ellipse,#mermaid-svg-fO2WGJvqDUwJk42C .node polygon,#mermaid-svg-fO2WGJvqDUwJk42C .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-fO2WGJvqDUwJk42C .rough-node .label text,#mermaid-svg-fO2WGJvqDUwJk42C .node .label text,#mermaid-svg-fO2WGJvqDUwJk42C .image-shape .label,#mermaid-svg-fO2WGJvqDUwJk42C .icon-shape .label{text-anchor:middle;}#mermaid-svg-fO2WGJvqDUwJk42C .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-fO2WGJvqDUwJk42C .rough-node .label,#mermaid-svg-fO2WGJvqDUwJk42C .node .label,#mermaid-svg-fO2WGJvqDUwJk42C .image-shape .label,#mermaid-svg-fO2WGJvqDUwJk42C .icon-shape .label{text-align:center;}#mermaid-svg-fO2WGJvqDUwJk42C .node.clickable{cursor:pointer;}#mermaid-svg-fO2WGJvqDUwJk42C .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-fO2WGJvqDUwJk42C .arrowheadPath{fill:#333333;}#mermaid-svg-fO2WGJvqDUwJk42C .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-fO2WGJvqDUwJk42C .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-fO2WGJvqDUwJk42C .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-fO2WGJvqDUwJk42C .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-fO2WGJvqDUwJk42C .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-fO2WGJvqDUwJk42C .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-fO2WGJvqDUwJk42C .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-fO2WGJvqDUwJk42C .cluster text{fill:#333;}#mermaid-svg-fO2WGJvqDUwJk42C .cluster span{color:#333;}#mermaid-svg-fO2WGJvqDUwJk42C div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-fO2WGJvqDUwJk42C .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-fO2WGJvqDUwJk42C rect.text{fill:none;stroke-width:0;}#mermaid-svg-fO2WGJvqDUwJk42C .icon-shape,#mermaid-svg-fO2WGJvqDUwJk42C .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-fO2WGJvqDUwJk42C .icon-shape p,#mermaid-svg-fO2WGJvqDUwJk42C .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-fO2WGJvqDUwJk42C .icon-shape .label rect,#mermaid-svg-fO2WGJvqDUwJk42C .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-fO2WGJvqDUwJk42C .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-fO2WGJvqDUwJk42C .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-fO2WGJvqDUwJk42C :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} preload() 完成
通过 Socket
收到 AMS 的 fork 请求
Zygote 进程
forkSystemServer()
Zygote(父进程)
→ runSelectLoop()
等 AMS 指令
SystemServer(子进程)
→ handleSystemServerProcess()
→ SystemServer.main()
启动 60+ 个系统服务
AMS 就绪
fork 出 App 进程
关键时序:Zygote 必须先 fork SystemServer → SystemServer 启动 AMS → AMS 就绪后才通知 Zygote "可以 fork App 了"。所以
runSelectLoop在 SystemServer 起来之后才真正收到第一个 App fork 请求。
三、SystemServer 的启动流程总览
SystemServer 的入口是 com.android.server.SystemServer.main(),整个启动分三个大阶段:
#mermaid-svg-YhCeNw5vyMKJm4ML{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-YhCeNw5vyMKJm4ML .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-YhCeNw5vyMKJm4ML .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-YhCeNw5vyMKJm4ML .error-icon{fill:#552222;}#mermaid-svg-YhCeNw5vyMKJm4ML .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-YhCeNw5vyMKJm4ML .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-YhCeNw5vyMKJm4ML .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-YhCeNw5vyMKJm4ML .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-YhCeNw5vyMKJm4ML .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-YhCeNw5vyMKJm4ML .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-YhCeNw5vyMKJm4ML .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-YhCeNw5vyMKJm4ML .marker{fill:#333333;stroke:#333333;}#mermaid-svg-YhCeNw5vyMKJm4ML .marker.cross{stroke:#333333;}#mermaid-svg-YhCeNw5vyMKJm4ML svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-YhCeNw5vyMKJm4ML p{margin:0;}#mermaid-svg-YhCeNw5vyMKJm4ML .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-YhCeNw5vyMKJm4ML .cluster-label text{fill:#333;}#mermaid-svg-YhCeNw5vyMKJm4ML .cluster-label span{color:#333;}#mermaid-svg-YhCeNw5vyMKJm4ML .cluster-label span p{background-color:transparent;}#mermaid-svg-YhCeNw5vyMKJm4ML .label text,#mermaid-svg-YhCeNw5vyMKJm4ML span{fill:#333;color:#333;}#mermaid-svg-YhCeNw5vyMKJm4ML .node rect,#mermaid-svg-YhCeNw5vyMKJm4ML .node circle,#mermaid-svg-YhCeNw5vyMKJm4ML .node ellipse,#mermaid-svg-YhCeNw5vyMKJm4ML .node polygon,#mermaid-svg-YhCeNw5vyMKJm4ML .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-YhCeNw5vyMKJm4ML .rough-node .label text,#mermaid-svg-YhCeNw5vyMKJm4ML .node .label text,#mermaid-svg-YhCeNw5vyMKJm4ML .image-shape .label,#mermaid-svg-YhCeNw5vyMKJm4ML .icon-shape .label{text-anchor:middle;}#mermaid-svg-YhCeNw5vyMKJm4ML .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-YhCeNw5vyMKJm4ML .rough-node .label,#mermaid-svg-YhCeNw5vyMKJm4ML .node .label,#mermaid-svg-YhCeNw5vyMKJm4ML .image-shape .label,#mermaid-svg-YhCeNw5vyMKJm4ML .icon-shape .label{text-align:center;}#mermaid-svg-YhCeNw5vyMKJm4ML .node.clickable{cursor:pointer;}#mermaid-svg-YhCeNw5vyMKJm4ML .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-YhCeNw5vyMKJm4ML .arrowheadPath{fill:#333333;}#mermaid-svg-YhCeNw5vyMKJm4ML .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-YhCeNw5vyMKJm4ML .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-YhCeNw5vyMKJm4ML .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YhCeNw5vyMKJm4ML .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-YhCeNw5vyMKJm4ML .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YhCeNw5vyMKJm4ML .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-YhCeNw5vyMKJm4ML .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-YhCeNw5vyMKJm4ML .cluster text{fill:#333;}#mermaid-svg-YhCeNw5vyMKJm4ML .cluster span{color:#333;}#mermaid-svg-YhCeNw5vyMKJm4ML div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-YhCeNw5vyMKJm4ML .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-YhCeNw5vyMKJm4ML rect.text{fill:none;stroke-width:0;}#mermaid-svg-YhCeNw5vyMKJm4ML .icon-shape,#mermaid-svg-YhCeNw5vyMKJm4ML .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YhCeNw5vyMKJm4ML .icon-shape p,#mermaid-svg-YhCeNw5vyMKJm4ML .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-YhCeNw5vyMKJm4ML .icon-shape .label rect,#mermaid-svg-YhCeNw5vyMKJm4ML .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YhCeNw5vyMKJm4ML .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-YhCeNw5vyMKJm4ML .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-YhCeNw5vyMKJm4ML :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} SystemServer.main()
- 创建主线程 Looper
Looper.prepareMainLooper()
2. 加载 libandroid_servers.so
3. ★ startBootstrapServices()
引导服务
4. ★ startCoreServices()
核心服务
5. ★ startOtherServices()
其他服务
6. Looper.loop()
进入消息循环
源码骨架:
java
// frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer {
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
Looper.prepareMainLooper(); // 主线程 Looper
System.loadLibrary("android_servers"); // Native 库
// ★ 阶段 1:引导服务------最底层的依赖
startBootstrapServices(t);
// ★ 阶段 2:核心服务------系统运行必需的
startCoreServices(t);
// ★ 阶段 3:其他服务------功能性的、不是核心必需的
startOtherServices(t);
Looper.loop(); // 进入消息循环
throw new RuntimeException("Main loop unexpectedly exited");
}
}
三个阶段有严格的顺序:后面的阶段依赖前面阶段启动的服务。Bootstrap 里起的服务,Core 阶段会用到;Core 里起的服务,Other 阶段会用到。顺序不能乱。
四、启动阶段一:Bootstrap 服务
Bootstrap 服务是最底层的依赖------别的服务启动之前必须先有它们。
java
private void startBootstrapServices(TimingsTraceAndSlog t) {
// 1. ★ Installer ------ 创建 /data 目录结构(dexopt 等)
Installer installer = mSystemServiceManager.startService(Installer.class);
// 2. ★ ActivityManagerService ------ 四大组件的大管家
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
// 3. ★ PowerManagerService ------ 电源管理
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
// 4. ★ DisplayManagerService ------ 显示管理
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
// 5. PackageManagerService ------ APK 管理(AMS 依赖它)
mPackageManagerService = PackageManagerService.main(mSystemContext, ...);
// 6. UserManagerService ------ 多用户管理
mSystemServiceManager.startService(UserManagerService.LifeCycle.class);
}
Bootstrap 阶段启动的服务:
| 服务 | 一句话干嘛的 |
|---|---|
| Installer | 创建 /data 下的 dex 缓存目录 |
| AMS | 管理 Activity/Service/Broadcast/Provider 生命周期 |
| PMS | 扫描和安装 APK、管理权限 |
| PowerManagerService | 电源管理、WakeLock |
| DisplayManagerService | 显示器管理 |
| UserManagerService | 多用户管理 |
AMS 在 Bootstrap 阶段就启动了,但它正式就绪要等到 Other 阶段------因为 AMS 依赖很多后面阶段的服务(比如 InputManagerService 在 Core 阶段才起)。
五、启动阶段二:Core 服务
Core 服务是系统运行必不可少的,但比 Bootstrap 服务高一层。
java
private void startCoreServices(TimingsTraceAndSlog t) {
// 1. ★ BatteryService ------ 电池状态
mSystemServiceManager.startService(BatteryService.class);
// 2. ★ UsageStatsService ------ App 使用统计
mSystemServiceManager.startService(UsageStatsService.class);
// 3. ★ WebViewUpdateService ------ WebView 更新
mSystemServiceManager.startService(WebViewUpdateService.class);
// 4. ★ BinderCallsStatsService ------ Binder 调用统计
mSystemServiceManager.startService(BinderCallsStatsService.class);
}
Core 阶段启动的关键服务:
| 服务 | 干嘛的 |
|---|---|
| BatteryService | 电池电量、充电状态 |
| UsageStatsService | App 使用时长统计 |
| WebViewUpdateService | WebView 更新管理 |
| BinderCallsStatsService | Binder 性能监控 |
Core 阶段服务比较少------大部分服务在 Other 阶段。
六、启动阶段三:Other 服务
Other 阶段是大头 ------几十个功能性服务都在这里启动。源码里这个方法有几百行。
java
private void startOtherServices(TimingsTraceAndSlog t) {
// ====== 窗口和输入 ======
// WMS ------ 必须先于 InputManagerService
wm = WindowManagerService.main(context, inputManager, ...);
mSystemServiceManager.startService(InputManagerService.class);
// ====== 网络相关 ======
mSystemServiceManager.startService(NetworkManagementService.class);
mSystemServiceManager.startService(ConnectivityService.class);
mSystemServiceManager.startService(WifiService.class);
// ====== 多媒体 ======
mSystemServiceManager.startService(AudioService.class);
mSystemServiceManager.startService(MediaRouterService.class);
// ====== 通知和状态栏 ======
mSystemServiceManager.startService(NotificationManagerService.class);
mSystemServiceManager.startService(StatusBarManagerService.class);
// ====== 存储 ======
mSystemServiceManager.startService(StorageManagerService.class);
// ====== 安全 ======
mSystemServiceManager.startService(DevicePolicyManagerService.class);
// ... 60+ 个服务逐一启动
// ★ 最后一步:调 AMS.systemReady()
// 告诉 AMS 所有服务都就绪了,可以开始管理 Activity 了
mActivityManagerService.systemReady(() -> {
// 回调:可以做"开机完成后"的事
startSystemUi(context); // 启动 SystemUI(状态栏)
});
}
Other 阶段启动的关键服务清单:
| 分类 | 服务名称 | 干嘛的 |
|---|---|---|
| 窗口 | WindowManagerService | 窗口层级、Surface 分配 |
| 输入 | InputManagerService | 触摸/按键事件分发 |
| 网络 | ConnectivityService | 网络连接管理 |
| 网络 | WifiService | Wi-Fi 管理 |
| 音频 | AudioService | 音量、音频路由 |
| 通知 | NotificationManagerService | 通知栏 |
| 多媒体 | MediaRouterService | 媒体路由(投屏) |
| 打印 | PrintManagerService | 打印服务 |
| 存储 | StorageManagerService | 存储设备管理 |
| 安全 | DevicePolicyManagerService | 设备策略管理 |
七、系统服务的依赖管理
60+ 个服务不是胡乱启动的------有严格的依赖关系。
核心依赖图:
#mermaid-svg-jo2zKV0XAG0oQsBd{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-jo2zKV0XAG0oQsBd .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-jo2zKV0XAG0oQsBd .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-jo2zKV0XAG0oQsBd .error-icon{fill:#552222;}#mermaid-svg-jo2zKV0XAG0oQsBd .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jo2zKV0XAG0oQsBd .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-jo2zKV0XAG0oQsBd .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jo2zKV0XAG0oQsBd .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jo2zKV0XAG0oQsBd .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-jo2zKV0XAG0oQsBd .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jo2zKV0XAG0oQsBd .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jo2zKV0XAG0oQsBd .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jo2zKV0XAG0oQsBd .marker.cross{stroke:#333333;}#mermaid-svg-jo2zKV0XAG0oQsBd svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jo2zKV0XAG0oQsBd p{margin:0;}#mermaid-svg-jo2zKV0XAG0oQsBd .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-jo2zKV0XAG0oQsBd .cluster-label text{fill:#333;}#mermaid-svg-jo2zKV0XAG0oQsBd .cluster-label span{color:#333;}#mermaid-svg-jo2zKV0XAG0oQsBd .cluster-label span p{background-color:transparent;}#mermaid-svg-jo2zKV0XAG0oQsBd .label text,#mermaid-svg-jo2zKV0XAG0oQsBd span{fill:#333;color:#333;}#mermaid-svg-jo2zKV0XAG0oQsBd .node rect,#mermaid-svg-jo2zKV0XAG0oQsBd .node circle,#mermaid-svg-jo2zKV0XAG0oQsBd .node ellipse,#mermaid-svg-jo2zKV0XAG0oQsBd .node polygon,#mermaid-svg-jo2zKV0XAG0oQsBd .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jo2zKV0XAG0oQsBd .rough-node .label text,#mermaid-svg-jo2zKV0XAG0oQsBd .node .label text,#mermaid-svg-jo2zKV0XAG0oQsBd .image-shape .label,#mermaid-svg-jo2zKV0XAG0oQsBd .icon-shape .label{text-anchor:middle;}#mermaid-svg-jo2zKV0XAG0oQsBd .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-jo2zKV0XAG0oQsBd .rough-node .label,#mermaid-svg-jo2zKV0XAG0oQsBd .node .label,#mermaid-svg-jo2zKV0XAG0oQsBd .image-shape .label,#mermaid-svg-jo2zKV0XAG0oQsBd .icon-shape .label{text-align:center;}#mermaid-svg-jo2zKV0XAG0oQsBd .node.clickable{cursor:pointer;}#mermaid-svg-jo2zKV0XAG0oQsBd .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-jo2zKV0XAG0oQsBd .arrowheadPath{fill:#333333;}#mermaid-svg-jo2zKV0XAG0oQsBd .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-jo2zKV0XAG0oQsBd .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-jo2zKV0XAG0oQsBd .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jo2zKV0XAG0oQsBd .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-jo2zKV0XAG0oQsBd .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jo2zKV0XAG0oQsBd .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-jo2zKV0XAG0oQsBd .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-jo2zKV0XAG0oQsBd .cluster text{fill:#333;}#mermaid-svg-jo2zKV0XAG0oQsBd .cluster span{color:#333;}#mermaid-svg-jo2zKV0XAG0oQsBd div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-jo2zKV0XAG0oQsBd .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-jo2zKV0XAG0oQsBd rect.text{fill:none;stroke-width:0;}#mermaid-svg-jo2zKV0XAG0oQsBd .icon-shape,#mermaid-svg-jo2zKV0XAG0oQsBd .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jo2zKV0XAG0oQsBd .icon-shape p,#mermaid-svg-jo2zKV0XAG0oQsBd .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-jo2zKV0XAG0oQsBd .icon-shape .label rect,#mermaid-svg-jo2zKV0XAG0oQsBd .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jo2zKV0XAG0oQsBd .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-jo2zKV0XAG0oQsBd .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-jo2zKV0XAG0oQsBd :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Installer
/data 目录
ActivityManagerService
DisplayManagerService
WindowManagerService
InputManagerService
NotificationManagerService
PowerManagerService
BatteryService
systemReady() 回调链:
很多服务在自己启动之后不会直接对外暴露 API,而是等所有服务都起来了才 systemReady。
java
// AMS 的 systemReady 里会调其他服务的 systemReady
mActivityManagerService.systemReady(() -> {
// 1. 启动 SystemUI
startSystemUi(context);
// 2. 通知 Watchdog 开始监控
Watchdog.getInstance().start();
// 3. 调其他服务的 systemReady()
mDisplayManagerService.systemReady(...);
mAppOpsService.systemReady();
mWindowManagerService.systemReady();
// 4. 开机流程走完了
// AMS 开始真正工作:启动 Launcher、恢复上次的 Activity 栈
});
八、Binder 注册:ServiceManager
每个系统服务启动后,都要去 ServiceManager 注册自己------这样其他进程才能通过 Binder 找到它。
#mermaid-svg-sDVObX2j7ThxIFhc{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-sDVObX2j7ThxIFhc .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sDVObX2j7ThxIFhc .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sDVObX2j7ThxIFhc .error-icon{fill:#552222;}#mermaid-svg-sDVObX2j7ThxIFhc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sDVObX2j7ThxIFhc .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sDVObX2j7ThxIFhc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sDVObX2j7ThxIFhc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sDVObX2j7ThxIFhc .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sDVObX2j7ThxIFhc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sDVObX2j7ThxIFhc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sDVObX2j7ThxIFhc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sDVObX2j7ThxIFhc .marker.cross{stroke:#333333;}#mermaid-svg-sDVObX2j7ThxIFhc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sDVObX2j7ThxIFhc p{margin:0;}#mermaid-svg-sDVObX2j7ThxIFhc .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-sDVObX2j7ThxIFhc .cluster-label text{fill:#333;}#mermaid-svg-sDVObX2j7ThxIFhc .cluster-label span{color:#333;}#mermaid-svg-sDVObX2j7ThxIFhc .cluster-label span p{background-color:transparent;}#mermaid-svg-sDVObX2j7ThxIFhc .label text,#mermaid-svg-sDVObX2j7ThxIFhc span{fill:#333;color:#333;}#mermaid-svg-sDVObX2j7ThxIFhc .node rect,#mermaid-svg-sDVObX2j7ThxIFhc .node circle,#mermaid-svg-sDVObX2j7ThxIFhc .node ellipse,#mermaid-svg-sDVObX2j7ThxIFhc .node polygon,#mermaid-svg-sDVObX2j7ThxIFhc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-sDVObX2j7ThxIFhc .rough-node .label text,#mermaid-svg-sDVObX2j7ThxIFhc .node .label text,#mermaid-svg-sDVObX2j7ThxIFhc .image-shape .label,#mermaid-svg-sDVObX2j7ThxIFhc .icon-shape .label{text-anchor:middle;}#mermaid-svg-sDVObX2j7ThxIFhc .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-sDVObX2j7ThxIFhc .rough-node .label,#mermaid-svg-sDVObX2j7ThxIFhc .node .label,#mermaid-svg-sDVObX2j7ThxIFhc .image-shape .label,#mermaid-svg-sDVObX2j7ThxIFhc .icon-shape .label{text-align:center;}#mermaid-svg-sDVObX2j7ThxIFhc .node.clickable{cursor:pointer;}#mermaid-svg-sDVObX2j7ThxIFhc .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-sDVObX2j7ThxIFhc .arrowheadPath{fill:#333333;}#mermaid-svg-sDVObX2j7ThxIFhc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-sDVObX2j7ThxIFhc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-sDVObX2j7ThxIFhc .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sDVObX2j7ThxIFhc .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-sDVObX2j7ThxIFhc .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sDVObX2j7ThxIFhc .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-sDVObX2j7ThxIFhc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-sDVObX2j7ThxIFhc .cluster text{fill:#333;}#mermaid-svg-sDVObX2j7ThxIFhc .cluster span{color:#333;}#mermaid-svg-sDVObX2j7ThxIFhc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-sDVObX2j7ThxIFhc .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-sDVObX2j7ThxIFhc rect.text{fill:none;stroke-width:0;}#mermaid-svg-sDVObX2j7ThxIFhc .icon-shape,#mermaid-svg-sDVObX2j7ThxIFhc .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sDVObX2j7ThxIFhc .icon-shape p,#mermaid-svg-sDVObX2j7ThxIFhc .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-sDVObX2j7ThxIFhc .icon-shape .label rect,#mermaid-svg-sDVObX2j7ThxIFhc .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sDVObX2j7ThxIFhc .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-sDVObX2j7ThxIFhc .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-sDVObX2j7ThxIFhc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} AMS 启动完成
ServiceManager.addService()
注册到 Binder 服务列表
ServiceManager
维护一个服务名 → Binder 对象的 Map
其他进程
ServiceManager.getService('activity')
拿到 AMS 的 Binder 引用
通过 Binder 调 AMS
(startActivity、bindService...)
ServiceManager 里的注册名:
| 注册名 | 服务 |
|---|---|
"activity" |
ActivityManagerService |
"window" |
WindowManagerService |
"package" |
PackageManagerService |
"power" |
PowerManagerService |
"display" |
DisplayManagerService |
"input" |
InputManagerService |
"audio" |
AudioService |
"battery" |
BatteryService |
九、实战:添加一个自定义系统服务
如果你做 ROM 定制,想在 SystemServer 里加一个自己的服务,需要注意正确注册并管理依赖。
第 1 步:定义 AIDL 接口
java
// frameworks/base/core/java/android/app/ICustomService.aidl
package android.app;
interface ICustomService {
void doSomething();
String getStatus();
}
第 2 步:实现服务端
java
// frameworks/base/services/core/java/com/android/server/custom/CustomService.java
package com.android.server.custom;
import android.app.ICustomService;
import android.util.Slog;
public class CustomService extends ICustomService.Stub {
private static final String TAG = "CustomService";
public CustomService() {
Slog.i(TAG, "CustomService created");
}
@Override
public void doSomething() {
Slog.i(TAG, "doSomething called");
}
@Override
public String getStatus() {
return "CustomService is running";
}
// ★ 生命周期方法(可选)
public void systemReady() {
Slog.i(TAG, "System ready, CustomService is ready too");
}
public void onBootPhase(int phase) {
// 接收开机阶段通知
}
}
第 3 步:在 SystemServer 里注册并启动
java
// frameworks/base/services/java/com/android/server/SystemServer.java
private void startOtherServices(TimingsTraceAndSlog t) {
// ... 前面各种服务
// ★ 加上自己的服务
try {
CustomService customService = new CustomService();
ServiceManager.addService("custom", customService);
mActivityManagerService.addCustomSystemService(
"custom", customService, "com.android.permission.CUSTOM"
);
} catch (Throwable e) {
Slog.e(TAG, "Failed to start CustomService", e);
}
// ... 后面继续
}
第 4 步:App 端调用
java
import android.app.ICustomService;
import android.os.ServiceManager;
import android.os.RemoteException;
// 通过 Binder 获取系统服务
ICustomService customService = ICustomService.Stub.asInterface(
ServiceManager.getService("custom")
);
if (customService != null) {
String status = customService.getStatus();
Log.d("MyApp", status);
}
自定义系统服务只能在系统签名的 App 里调用。普通 App 没有权限访问自定义系统服务,除非你在 SELinux 策略里给它授权。
十、实战:看看你手机里跑了多少系统服务
bash
# 方式 1:通过 dumpsys 看所有服务(非常多)
adb shell dumpsys -l
# 方式 2:看 SystemServer 进程里有多少线程
adb shell ps -A | grep system_server
# 记下 PID,然后:
adb shell cat /proc/<PID>/status | grep Threads
# 方式 3:看 ServiceManager 里注册了哪些服务
adb shell service list
# 输出片段:
# 1 activity: [android.app.IActivityManager]
# 3 package: [android.content.pm.IPackageManager]
# 5 window: [android.view.IWindowManager]
# 7 power: [android.os.IPowerManager]
# 10 display: [android.hardware.display.IDisplayManager]
# ...
service list数是 SystemServer 里真正注册到 Binder 的服务数量。一般有 80~120 个不等,跟 Android 版本和设备配置有关。
十一、常见踩坑记录
坑 1:自定义服务启动顺序搞错,依赖的服务还没起来
你的 CustomService 构造时如果调了 AMS 或 WMS,但 AMS 还没注册完------直接 NullPointerException。
java
// ❌ 在 startBootstrapServices 阶段注册自定义服务
// 这时候 WMS 还没起,调 WMS 必崩
// ✓ 在 startOtherServices 的合适位置注册
// 确保你依赖的服务已经启动了
坑 2:主线程阻塞导致开机慢
SystemServer 的启动是单线程串行 的。你的 systemReady 里做了耗时操作(比如读文件、初始化数据库),会拖慢整个开机流程。
java
// ❌ systemReady 里做同步 IO
public void systemReady() {
File file = new File("/data/custom/config.json");
String content = readFileSync(file); // 阻塞主线程
}
// ✓ 扔到子线程
public void systemReady() {
new Thread(() -> {
readFileSync(new File("/data/custom/config.json"));
}).start();
}
坑 3:SELinux 权限没给够,注册服务被拒绝
ServiceManager.addService 在 Enforcing 模式下需要 SELinux 权限:
bash
adb logcat -b all | grep "avc.*denied.*add_service"
如果看到这类日志,需要补 te 文件。
坑 4:SystemServer 进程 OOM
SystemServer 里跑了 60+ 个服务,而且用的是主线程 Looper。如果某个服务内存泄漏,SystemServer 的堆就越来越大,最终系统杀 SystemServer → 重启 → 相当于软重启,用户看到的是卡顿然后自动重启。
排查方式:
bash
adb shell dumpsys meminfo system_server
看 TOTAL PSS 是否异常增长。
坑 5:服务注册名重复
自定义服务的注册名如果和已有服务冲突,后注册的会覆盖前面的,导致前面的服务找不到。
java
// ❌ "activity" 已经被 AMS 占了
ServiceManager.addService("activity", myService);
// ✓ 用自己独特的名字
ServiceManager.addService("custom_xyz", myService);
十二、总结
SystemServer 启动流程一张图总结:
#mermaid-svg-cEe2wrRVA7VhdHTe{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-cEe2wrRVA7VhdHTe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-cEe2wrRVA7VhdHTe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-cEe2wrRVA7VhdHTe .error-icon{fill:#552222;}#mermaid-svg-cEe2wrRVA7VhdHTe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cEe2wrRVA7VhdHTe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-cEe2wrRVA7VhdHTe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cEe2wrRVA7VhdHTe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cEe2wrRVA7VhdHTe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-cEe2wrRVA7VhdHTe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cEe2wrRVA7VhdHTe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cEe2wrRVA7VhdHTe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cEe2wrRVA7VhdHTe .marker.cross{stroke:#333333;}#mermaid-svg-cEe2wrRVA7VhdHTe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cEe2wrRVA7VhdHTe p{margin:0;}#mermaid-svg-cEe2wrRVA7VhdHTe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-cEe2wrRVA7VhdHTe .cluster-label text{fill:#333;}#mermaid-svg-cEe2wrRVA7VhdHTe .cluster-label span{color:#333;}#mermaid-svg-cEe2wrRVA7VhdHTe .cluster-label span p{background-color:transparent;}#mermaid-svg-cEe2wrRVA7VhdHTe .label text,#mermaid-svg-cEe2wrRVA7VhdHTe span{fill:#333;color:#333;}#mermaid-svg-cEe2wrRVA7VhdHTe .node rect,#mermaid-svg-cEe2wrRVA7VhdHTe .node circle,#mermaid-svg-cEe2wrRVA7VhdHTe .node ellipse,#mermaid-svg-cEe2wrRVA7VhdHTe .node polygon,#mermaid-svg-cEe2wrRVA7VhdHTe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-cEe2wrRVA7VhdHTe .rough-node .label text,#mermaid-svg-cEe2wrRVA7VhdHTe .node .label text,#mermaid-svg-cEe2wrRVA7VhdHTe .image-shape .label,#mermaid-svg-cEe2wrRVA7VhdHTe .icon-shape .label{text-anchor:middle;}#mermaid-svg-cEe2wrRVA7VhdHTe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-cEe2wrRVA7VhdHTe .rough-node .label,#mermaid-svg-cEe2wrRVA7VhdHTe .node .label,#mermaid-svg-cEe2wrRVA7VhdHTe .image-shape .label,#mermaid-svg-cEe2wrRVA7VhdHTe .icon-shape .label{text-align:center;}#mermaid-svg-cEe2wrRVA7VhdHTe .node.clickable{cursor:pointer;}#mermaid-svg-cEe2wrRVA7VhdHTe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-cEe2wrRVA7VhdHTe .arrowheadPath{fill:#333333;}#mermaid-svg-cEe2wrRVA7VhdHTe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-cEe2wrRVA7VhdHTe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-cEe2wrRVA7VhdHTe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-cEe2wrRVA7VhdHTe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-cEe2wrRVA7VhdHTe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-cEe2wrRVA7VhdHTe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-cEe2wrRVA7VhdHTe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-cEe2wrRVA7VhdHTe .cluster text{fill:#333;}#mermaid-svg-cEe2wrRVA7VhdHTe .cluster span{color:#333;}#mermaid-svg-cEe2wrRVA7VhdHTe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-cEe2wrRVA7VhdHTe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-cEe2wrRVA7VhdHTe rect.text{fill:none;stroke-width:0;}#mermaid-svg-cEe2wrRVA7VhdHTe .icon-shape,#mermaid-svg-cEe2wrRVA7VhdHTe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-cEe2wrRVA7VhdHTe .icon-shape p,#mermaid-svg-cEe2wrRVA7VhdHTe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-cEe2wrRVA7VhdHTe .icon-shape .label rect,#mermaid-svg-cEe2wrRVA7VhdHTe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-cEe2wrRVA7VhdHTe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-cEe2wrRVA7VhdHTe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-cEe2wrRVA7VhdHTe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Zygote fork
SystemServer
SystemServer.main()
Looper.prepareMainLooper()
startBootstrapServices()
Installer / AMS / PMS
Power / Display
startCoreServices()
Battery / UsageStats
WebView / BinderStats
startOtherServices()
WMS / Input / Audio / Wifi
Notification / Storage ...
★ 60+ 个服务
AMS.systemReady()
→ 启动 SystemUI
→ 启动 Launcher
→ 开机完成
Looper.loop()
三个阶段的职责速查:
| 阶段 | 多少个服务 | 典型服务 | 核心特征 |
|---|---|---|---|
| Bootstrap | ~6 | AMS / PMS / Power / Display | 最底层依赖,后面的服务全依赖它们 |
| Core | ~4 | Battery / UsageStats | 系统运行必需 |
| Other | ~60+ | WMS / Audio / Wifi / Notification ... | 功能服务,数量最多 |
核心源码位置:
| 文件 | 位置 |
|---|---|
| SystemServer.java | frameworks/base/services/java/com/android/server/SystemServer.java |
| SystemServiceManager | frameworks/base/services/core/java/com/android/server/SystemServiceManager.java |
| ServiceManager | frameworks/base/core/java/android/os/ServiceManager.java |
小结: SystemServer 说白了就是一个服务启动器------按顺序把 60+ 个服务一个一个创建、初始化、注册到 ServiceManager,最后调 AMS.systemReady() 把接力棒交给 AMS,开机就算完成了。你加的任何一个系统服务,都是在这三个阶段的合适位置塞进去的。