Mobile processors consume massive amounts of battery power. The fundamental philosophy of the Android operating system is that the CPU should be completely asleep (suspended) as often as possible.
The PowerManagerService (often abbreviated as PMS, not to be confused with PackageManagerService) is the absolute authority on when the device is awake, when the screen is on, and when the CPU is allowed to sleep.
Wake Lock Management
When the screen turns off, the Linux kernel immediately attempts to suspend the CPU to save power. If an app is downloading a massive 5GB file in the background, the CPU going to sleep would instantly break the network connection.
To prevent this, the app must request a Wake Lock from the PowerManagerService.
- The app calls
PowerManager.newWakeLock(PARTIAL_WAKE_LOCK). - The
PowerManagerServicereceives the Binder call. It registers the app in its internal list of active wake locks. - The
PowerManagerServicethen makes a JNI (Java Native Interface) call down to the Linux kernel, writing to the/sys/power/wake_locknode. - The kernel sees the active lock and physically prevents the CPU hardware from entering the low-power suspend state.
// Example of an app requesting a Partial Wake Lock to keep the CPU running
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::DownloadLock");
wakeLock.acquire(10 * 60 * 1000L); // Hold lock for max 10 minutes
// ... perform download ...
wakeLock.release();
Screen On/Off Flow
Turning the screen on and off is not as simple as cutting power to the display. It requires massive coordination across the framework.
When you press the physical power button to wake the device:
- The hardware interrupt is caught by the
InputManagerService. - The
InputManagerServicepasses the power button event to thePhoneWindowManager(the policy engine of WMS). - The policy engine tells the
PowerManagerServiceto wake up the device. - The
PowerManagerServicechanges its internal state toAWAKE. - It commands the Display HAL to physically turn on the OLED panel.
- It notifies the
ActivityManagerServicethat the screen is on, prompting AMS to move the top Activity into theRESUMEDstate so it can start rendering UI frames.
# Debugging tool for platform engineers to trace exactly what is keeping the device awake
adb shell dumpsys power
DreamManager and ScreenTimeout
The PowerManagerService constantly runs internal timers based on user settings (e.g., "Screen timeout after 30 seconds").
When the user stops touching the screen, the timer counts down. If it reaches zero, PMS begins the sleep sequence. Before turning off the screen entirely, it checks with the DreamManagerService to see if a screen saver (a "Daydream") is configured to run while the device is docked or charging. If not, it dims the screen and eventually issues the suspend command to the Linux kernel.