0%

安卓跨进程通信Binder机制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
📘 Android Binder 机制通俗原理全解析(流程图 + AIDL 示例)

Binder 是 Android 系统最核心的机制之一。所有系统服务(AMS、WMS、PackageManager、MediaService…)背后几乎都通过 Binder 完成通信,因此如果想真正理解 Android 系统原理,Binder 是绕不过去的基础。

本文通过通俗解释 + 配图 + 代码,快速理解 Binder 的本质与工作机制。

⭐ 1. 为什么 Android 一定要用 Binder?

一句话解释:

Android 是多进程系统,进程之间必须能通信,Binder 是它选中的 IPC 方案。

🔍 为什么要多进程?

每个 App 独立进程 → 防止互相崩溃

系统服务独立进程(system_server)

UI 服务 (SurfaceFlinger)

媒体服务(MediaService)

所以:App 想启动 Activity、申请权限、绘制 UI…全部要通过跨进程调用。

传统 IPC(Socket/管道/共享内存)都不够好,Binder 胜出。

⭐ 2. Binder 有多强?(对比传统 IPC)
IPC 方式 拷贝次数 性能 安全性 使用复杂度
Socket 2 中 低 高
管道 pipe 2 中 低 中
共享内存 1 高 低 高(需同步)
❗ Binder 1 高 高(自动携带UID/PID) 低(系统封装)

Binder 为什么快?

Binder 只进行一次“用户态 → 内核态”的数据拷贝
其他 IPC 都需要“两次拷贝”。

⭐ 3. Binder 机制大图(核心流程)

以下是最常见的 “App 启动 Activity” 的 Binder 调用流程:

+-----------------+ +----------------+
| Client | | Server |
| (App 进程) | | (AMS等服务) |
+--------+--------+ +--------+-------+
| ^
| Proxy 调用 |
v |
+--------+--------+ +--------+-------+
| Binder 驱动 | <----> | Stub(服务端)|
| (内核 Kernel) | | 业务逻辑 |
+-----------------+ +----------------+


再看更完整的带 ServiceManager 的图:

获取服务 注册服务
┌──────────────┐ ┌───────────────┐
│ Client │ --查询服务--> │ ServiceManager │
└──────────────┘ └───────────────┘
│ │
│ 调用服务 │
v v
┌──────────────┐ Binder ┌──────────────┐
│ ClientProxy │ <-------------> │ ServerStub │
└──────────────┘ └──────────────┘


✔ Proxy:客户端代理
✔ Stub:服务端接收者
✔ ServiceManager:系统服务目录
✔ Binder Driver:内核通信枢纽

⭐ 4. Binder 的四大组件结构
角色 作用
Binder 驱动(内核) 负责数据传输、线程管理、内存映射
ServiceManager 类似“系统服务注册中心”
Server(Stub) 服务端对象,处理真正的业务逻辑
Client(Proxy) 客户端代理,负责发起 Binder 调用

你写 AIDL 时生成的就是:

Stub.java(服务端)

Proxy.java(客户端)

⭐ 5. AIDL 示例:最小可运行 Binder Demo

下面以一个简单的 AIDL 文件演示:

📄 5.1 AIDL 文件(IRemoteService.aidl)
package com.demo.ipc;

// AIDL 接口
interface IRemoteService {
int add(int a, int b);
}


编译后自动生成:

IRemoteService.Stub(服务端)

IRemoteService.Stub.Proxy(客户端)

📄 5.2 服务端实现(Stub)
public class RemoteService extends Service {

private final IRemoteService.Stub binder = new IRemoteService.Stub() {
@Override
public int add(int a, int b) {
return a + b;
}
};

@Override
public IBinder onBind(Intent intent) {
return binder;
}
}

📄 5.3 客户端调用(Proxy)
private IRemoteService remoteService;

private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
remoteService = IRemoteService.Stub.asInterface(service);
try {
int result = remoteService.add(3, 5);
Log.d("IPC", "结果:" + result);
} catch (RemoteException e) {
e.printStackTrace();
}
}

@Override
public void onServiceDisconnected(ComponentName name) {
remoteService = null;
}
};


绑定:

Intent intent = new Intent();
intent.setClassName("com.demo.server",
"com.demo.server.RemoteService");
bindService(intent, connection, BIND_AUTO_CREATE);


✔ 完整的 Binder Demo 至此全部跑通。

⭐ 6. Binder 为什么安全?

每次 Binder 调用都自带调用者 UID/PID(由内核提供),因此 AMS/WMS 能做:

权限校验

角色校验

进程身份确认

不可伪造 UID → 无法伪造系统身份

⭐ 7. Binder 为什么快?

关键原因:

一次拷贝 + 内核共享内存

驱动调度(队列 + 线程池)

零上下文切换同步(不用共享锁)

这是“低耗、高吞吐”的本质。

⭐ 8. 总结

Binder = 高性能 IPC + 安全校验 + RPC 封装 + 系统服务基础设施
Android 系统的运行效率、安全性和架构清晰度,几乎都依赖 Binder。

如果你想真正理解 Android:

✔ AMS 怎么启动 Activity
✔ WMS 怎么管理窗口
✔ MediaServer 为什么这么快
✔ PackageManagerService 如何校验 APK

全部要从 Binder 入门。