Progress · 0/7 phases
⚙️ ⚙️ Phase 5 — Framework Internals (AMS, WMS, PMS)
4 min read · Notion
Duration: 4–6 weeks | Level: Advanced
The Android Framework is the bridge between your app and the OS. Understanding it makes you a 10x Android engineer.
🏛️ The Big Three System Services
| Service | Acronym | Responsibility |
|---|---|---|
| ActivityManagerService | AMS | Manages Activities, app lifecycle, processes |
| WindowManagerService | WMS | Manages windows, layers, display |
| PackageManagerService | PMS | Manages APK install/uninstall, permissions, component resolution |
All three live in frameworks/base/services/core/java/com/android/server/
⏳ ActivityManagerService (AMS)
AMS is the most complex service in Android. It manages:
- Activity stack management (back stack, tasks)
- Starting / killing app processes
- Broadcasting (
sendBroadcast) - Starting Services
- App lifecycle (foreground/background)
- Process memory pressure (LRU cache of processes)
startActivity() deep dive
Activity.startActivity(intent)
↓
Instrumentation.execStartActivity()
↓
ActivityTaskManager.getService().startActivity() # Binder call to AMS
↓
ActivityTaskManagerService.startActivity()
↓
ActivityStarter.execute()
↓
RootWindowContainer.resumeFocusedTasksTopActivities()
↓
ActivityRecord.makeActiveIfNeeded() → realStartActivityLocked()
↓
ClientTransaction sent to app process via Binder
↓
ActivityThread handles LaunchActivityItem
↓
Instrumentation.callActivityOnCreate() → Activity.onCreate()Key AMS files
frameworks/base/services/core/java/com/android/server/am/
├── ActivityManagerService.java # Main service
├── ProcessRecord.java # Represents one app process
├── BroadcastQueue.java # Broadcast dispatch
├── ActiveServices.java # Service lifecycle
└── OomAdjuster.java # Process kill decisions
frameworks/base/services/core/java/com/android/server/wm/
├── ActivityTaskManagerService.java # Activity-specific (split from AMS in Android 10)
├── ActivityRecord.java # Represents one Activity instance
├── Task.java # Back stack task
└── ActivityStarter.java # Start activity logic🪟 WindowManagerService (WMS)
WMS manages every window on screen:
- Assigns Z-order (layers) to windows
- Handles input event routing to correct window
- Coordinates with SurfaceFlinger (native compositor) for rendering
- Manages window animations and transitions
Window hierarchy
Display
└─ RootDisplayArea
├─ StatusBar window
├─ NavigationBar window
└─ AppWindowToken (per app)
└─ WindowState (per window surface)SurfaceFlinger relationship
- WMS manages which windows exist and their properties
- SurfaceFlinger (native, C++) does the actual compositing (merging layers into final pixels)
- Apps draw into their
Surfacebuffers; SurfaceFlinger reads them
Key files:
frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp📦 PackageManagerService (PMS)
PMS handles everything related to installed apps:
- Parsing
AndroidManifest.xmlof every installed APK - Storing component info (Activities, Services, Receivers, Providers)
- Resolving intents to components
- Enforcing permissions
- APK install/uninstall pipeline
APK install flow
User taps "Install"
↓
PackageInstaller UI → PackageInstallerSession
↓
PMS.installPackage()
↓
PackageParser.parsePackage() # Parses AndroidManifest.xml
↓
dex2oat (ART compilation of .dex files)
↓
Copy to /data/app/<package>/
↓
Update package database (packages.xml)
↓
Broadcast ACTION_PACKAGE_ADDED🔥 Other Important Framework Components
InputManagerService
- Reads raw input events from kernel (
/dev/input/eventX) - Routes touch/key events to the focused window via WMS
- Key file:
frameworks/base/services/core/java/com/android/server/input/InputManagerService.java
ContentProvider & ContentResolver
- IPC mechanism for structured data sharing between apps
- Uses Binder under the hood
- SQLite + URI-based query interface
BroadcastReceiver dispatch
sendBroadcast(intent)
↓
AMS.broadcastIntent()
↓
BroadcastQueue.enqueueOrderedBroadcastLocked()
↓
BroadcastQueue.processNextBroadcast()
↓
App process → ActivityThread.handleReceiver()
↓
Receiver.onReceive()🛠️ Hands-On Tasks
- Add a log to
ActivityManagerService.javainstartActivity()and trace a real launch. - Add a log to
PackageManagerService.javainscanPackageLI()— watch it duringadb install. - Read
OomAdjuster.java— understand how the system decides which process to kill. - Use
adb shell dumpsys activityto inspect live AMS state. - Use
adb shell dumpsys windowto inspect live WMS window state. - Use
adb shell dumpsys package <your.package>to see parsed manifest info.
✅ Phase 5 Checklist
- Can explain the roles of AMS, WMS, and PMS
- Traced
startActivity()from app toActivity.onCreate() - Read
ActivityRecord.javaandTask.java - Understand window hierarchy in WMS
- Know SurfaceFlinger's role vs WMS
- Traced APK install flow through PMS
- Understand
OomAdjuster— how processes are killed - Understand
BroadcastQueuedispatch - Used
dumpsys activity,dumpsys window,dumpsys package - Modified a system service and built incrementally