Binder is the core mechanism for Inter-Process Communication (IPC) in Android systems and one of Android's distinctive features.
Why Android Chooses Binder
| Method | Pros | Cons |
|---|---|---|
| Pipe | Simple | Unidirectional, low efficiency |
| Socket | Universal | High overhead, slow |
| Shared Memory | Fast | Complex synchronization, poor security |
| Binder | Efficient, secure, easy to use | Steep learning curve |
Core Advantages of Binder
-
Efficiency
- Only one memory copy needed (traditional IPC needs two)
- Based on C/S architecture, high communication efficiency
-
Security
- Kernel-level process identity verification (UID/PID)
- Supports establishing private channels
-
Usability
- Encapsulates complex underlying implementation
- Provides high-level interfaces like AIDL
How Binder Works
1. Core Components
- Binder Driver: Located in kernel space, manages Binder communication
- ServiceManager: Manages registration and query of all system services
- Client/Server: Both parties in communication
2. Memory Mapping Mechanism
shellSender Process Binder Driver Receiver Process | | | | Data | Memory Map(MMAP) | | -------------> | ----------------> | | User Space | Kernel Space | User Space
- Sender copies data to kernel space
- Receiver accesses directly through memory mapping, no second copy needed
3. Communication Flow
shell1. Server registers service → ServiceManager 2. Client queries service → ServiceManager 3. Client obtains Server's Binder proxy 4. Client calls Server methods through Binder proxy 5. Binder Driver completes data transfer
Binder Applications in Android
ActivityManagerService (AMS)
- Manages Activity lifecycle
- Process scheduling
WindowManagerService (WMS)
- Window management
- Screen display
PackageManagerService (PMS)
- Application package management
- Permission management
AIDL (Android Interface Definition Language)
java// IRemoteService.aidl interface IRemoteService { int add(int a, int b); String getMessage(); }
AIDL compilation automatically generates:
- Stub: Server-side implementation
- Proxy: Client-side proxy
Key Points
- Binder is Android-specific IPC mechanism
- Understand one-copy memory mapping principle
- Master basic AIDL usage
- Binder thread pool defaults to 16 threads
- Note Binder communication data size limit (around 1MB)