Android Background Execution Reality

Understanding how Android handles background processes is critical for building reliable P2P applications. This guide explains Android's restrictions and how LiteP2P is designed to work within them.

Background Restrictions

Android enforces strict limits on background applications to preserve battery life and system resources. These restrictions affect:

  • Background CPU – Limited processing time for background apps
  • Background Networking – Network access may be delayed or blocked
  • Long-lived Sockets – TCP connections are terminated in background
  • Wake Locks – Restricted to prevent battery drain
  • Background Services – Heavily restricted since Android 8.0

Application States

Android treats applications differently based on their current state. Understanding these states is essential for proper LiteP2P integration.

State Behavior P2P Impact
Foreground No restrictions Full P2P functionality
Foreground Service High priority, user-visible Reliable connections maintained
Background Limited CPU & network Connections may drop
Doze Network suspended All connections dropped
App Standby Severe throttling No P2P activity
OEM Kill Process terminated Complete shutdown
Key Rule

LiteP2P must never assume persistent connectivity. The engine is designed to handle connection drops gracefully and reconnect when the app becomes active.

LiteP2P Execution Model

LiteP2P follows a wake–connect–sync–sleep model that works with Android's power management instead of against it.

Push Notification App Wakes Re-establish P2P Links Exchange Data Sleep

How It Works

  1. Push Notification Arrives – FCM or UnifiedPush delivers a wake signal
  2. App Wakes Up – Android gives the app a short execution window
  3. Re-establish Connections – LiteP2P quickly reconnects to necessary peers
  4. Exchange Data – Sync messages, files, or state with peers
  5. Return to Sleep – Release resources and allow the device to sleep
Design Philosophy

LiteP2P is not a background server. It's designed to wake up, sync, and sleep – maximizing battery efficiency while maintaining P2P functionality.

Understanding Doze Mode

Doze mode is Android's aggressive power-saving feature that activates when the device is stationary and unplugged.

What Doze Does

  • Suspends all network access
  • Defers alarms and jobs
  • Restricts GPS and Wi-Fi scans
  • Batches syncs to maintenance windows

LiteP2P Doze Strategy

LiteP2P handles Doze by:

  • Using high-priority FCM messages that can break through Doze
  • Performing quick syncs during maintenance windows
  • Caching outgoing messages until the device exits Doze
  • Gracefully handling connection drops when entering Doze

Best Practices

Use foreground services for active P2P operations
Implement push notifications for reliable wake-up
Design for intermittent connectivity
Cache messages for later delivery
Request battery optimization exemption