Skip to main content
Wake locks are Android power management tools that prevent the system from entering sleep states. React Native Background Guardian provides two types of wake locks: partial wake locks for background CPU execution and screen wake locks for keeping the display on.

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
Wake locks consume significant battery power. Always release them when your task is complete, and use appropriate timeouts to prevent battery drain if your app crashes.

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

The tag parameter is an optional identifier for debugging:
The tag appears in system logs and ADB output, making it easier to identify which component is holding the wake lock.
Internally, the library creates wake lock tags in the format BackgroundGuardian:WakeLock:YourTag. From BackgroundGuardianModule.kt:77:

Timeout parameter

The timeout parameter sets an automatic release time in milliseconds:
Always set an appropriate timeout! If your app crashes while holding a wake lock without a timeout, the CPU will remain active indefinitely, draining the battery. The default 24-hour timeout prevents this, but shorter timeouts are better for battery life.

How it works internally

On Android, partial wake locks are implemented using PowerManager.WakeLock with the PARTIAL_WAKE_LOCK flag:
On iOS, 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:
The implementation safely handles cases where no wake lock was acquired:

Checking wake lock status

You can check if a wake lock is currently held:
This is useful for updating UI state or debugging:
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:
From the implementation:
Important implication: If you need multiple concurrent wake locks for different components, you’ll need to implement your own 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 use FLAG_KEEP_SCREEN_ON:
This flag only affects the current Activity and is automatically cleared when the Activity is destroyed.

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:
See the Doze and App Standby page for details.

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. For long-running tasks, combine wake locks with a foreground service to prevent the system from considering your app idle: