Understanding wake locks
Android devices aggressively conserve power by entering sleep states when idle. Wake locks allow apps to signal that they need the device to stay awake:- Partial wake lock: Keeps the CPU running but allows the screen to turn off
- Screen wake lock: Keeps the screen on while the app is in the foreground
Partial wake locks
Partial wake locks keep the CPU running while allowing the screen, keyboard backlight, and other components to turn off. This is essential for background processing.When to use partial wake locks
Use partial wake locks for:- Background audio playback - Music or podcast players
- Long-running downloads - File downloads or sync operations
- Location tracking - Continuous GPS tracking
- Real-time data processing - Sensor data collection and processing
- Network operations - WebSocket connections or continuous polling
Acquiring a partial wake lock
Parameters explained
Tag parameter
Thetag parameter is an optional identifier for debugging:
Internally, the library creates wake lock tags in the format
BackgroundGuardian:WakeLock:YourTag. From BackgroundGuardianModule.kt:77:Timeout parameter
Thetimeout parameter sets an automatic release time in milliseconds:
How it works internally
On Android, partial wake locks are implemented usingPowerManager.WakeLock with the PARTIAL_WAKE_LOCK flag:
acquireWakeLock() is a no-op that returns true immediately, as iOS handles background execution differently through Background Modes configured in Xcode.
Releasing wake locks
Always release wake locks when your background work is complete:Checking wake lock status
You can check if a wake lock is currently held:On Android,
isWakeLockHeld() checks the actual wake lock state. On iOS, it always returns false as wake locks don’t exist on that platform.Reference counting behavior
The library uses a single-instance wake lock model, not reference counting:Screen wake locks
Screen wake locks keep the display on while the app is in the foreground. This is different from partial wake locks and does not keep the CPU running in the background.When to use screen wake locks
Use screen wake locks for:- Video playback - Prevent screen timeout during videos
- Recipe apps - Keep screen on while cooking
- Navigation apps - Display always visible during directions
- Kiosk mode - Public display terminals
- Presentation mode - Slides or dashboards
- Reading apps - Prevent screen timeout while reading
Enabling screen wake lock
Disabling screen wake lock
Always disable the screen wake lock when the user is done:Platform-specific behavior
Android
On Android, screen wake locks useFLAG_KEEP_SCREEN_ON:
iOS
On iOS, screen wake locks disable the idle timer:Screen wake locks only work while the app is in the foreground. They do not replace partial wake locks for background CPU execution. Use
acquireWakeLock() for background tasks.Complete usage examples
Background audio player
Video player with screen wake lock
Location tracking with wake lock
Kiosk mode (always-on display)
Best practices
1. Always use timeouts
Never acquire a wake lock without a timeout:2. Release in finally blocks
Always release wake locks, even if errors occur:3. Use descriptive tags
Use meaningful tags for easier debugging:4. Check wake lock status
Verify wake lock status before critical operations:5. Combine with battery optimization exemptions
For reliable background execution, combine wake locks with battery optimization exemptions:6. Monitor battery impact
Track wake lock usage and inform users about battery impact:Debugging wake locks
You can verify wake lock behavior using ADB:Check active wake locks
Monitor wake lock acquisition
Check battery stats
Limitations
1. Doze mode interference
Wake locks may be ignored during Doze mode unless you’re exempt from battery optimizations:2. Power Save mode restrictions
Wake locks are less effective when Power Save (Battery Saver) mode is active:3. OEM-specific killing
Wake locks don’t prevent OEM-specific app killers from terminating your app. See the OEM Restrictions page.4. Foreground services recommended
For long-running tasks, combine wake locks with a foreground service to prevent the system from considering your app idle:Related topics
- Doze and App Standby - How Doze mode affects wake locks
- Battery Optimization - Exemptions for reliable background execution
- OEM Restrictions - Manufacturer-specific battery management

