Battery Optimization (Critical)
Battery optimization is one of the most critical aspects of LiteP2P Android integration. Improper handling leads to connection failures and paradoxically higher battery drain.
Without proper battery optimization handling, LiteP2P connections will fail unpredictably, and your app may be killed by the OS or OEM firmware.
What Battery Optimization Does
Android's battery optimization system applies aggressive restrictions to apps it considers "not in use":
- Delays background execution – Your app may not run when expected
- Blocks background network – Network requests are queued or dropped
- Kills idle processes – Your app is terminated to free memory
- Coalesces alarms – Scheduled tasks are batched together
- Drops sockets silently – TCP connections are closed without notification
OEMs like Xiaomi, Oppo, Vivo, and Samsung apply additional undocumented restrictions beyond stock Android. See the OEM Behavior guide for details.
Why LiteP2P Needs Exemption
LiteP2P relies on several capabilities that battery optimization restricts:
| LiteP2P Requirement | Battery Optimization Impact |
|---|---|
| Short wake windows | Delayed or skipped entirely |
| Timely reconnections | Blocked by network restrictions |
| Secure handshakes | Timeout during CPU throttling |
| Push notification handling | Delayed delivery |
The Paradox
Battery optimization causes:
- Missed sync windows – App can't connect when needed
- Excessive retries – Failed connections lead to more attempts
- Higher battery drain overall – More work, less efficiency
Properly exempted apps with LiteP2P's efficient wake-sync-sleep model actually use less battery than throttled apps that constantly retry failed operations.
Requesting Exemption
Applications must encourage users to disable battery optimization for reliable P2P functionality.
Check Current Status
fun isBatteryOptimizationDisabled(context: Context): Boolean {
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
return powerManager.isIgnoringBatteryOptimizations(context.packageName)
}
Request Exemption
fun requestBatteryOptimizationExemption(context: Context) {
if (!isBatteryOptimizationDisabled(context)) {
val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = Uri.parse("package:${context.packageName}")
}
context.startActivity(intent)
}
}
// Add to manifest
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
User-Friendly Prompt
fun showBatteryOptimizationDialog(activity: Activity) {
if (isBatteryOptimizationDisabled(activity)) return
AlertDialog.Builder(activity)
.setTitle("Enable Reliable Sync")
.setMessage(
"To ensure reliable peer-to-peer connectivity, please disable " +
"battery optimization for this app. This allows the app to sync " +
"data even when running in the background."
)
.setPositiveButton("Enable") { _, _ ->
requestBatteryOptimizationExemption(activity)
}
.setNegativeButton("Later", null)
.show()
}
LiteP2P Built-in Helper
LiteP2P provides convenience methods for battery optimization handling:
// Check and request in one call
LiteP2P.ensureBatteryOptimizationExemption(context) { granted ->
if (granted) {
Log.d("LiteP2P", "Battery optimization disabled - full functionality")
} else {
Log.w("LiteP2P", "Battery optimization enabled - limited background sync")
}
}
// Show built-in educational UI
LiteP2P.showBatteryOptimizationEducation(activity)
Google Play Policy
Google allows apps to request battery optimization exemption if they have a legitimate need. P2P networking qualifies under:
- Communication apps requiring real-time messaging
- File sync applications
- Decentralized/P2P applications
When requesting exemption, clearly explain to users why it's needed. Be transparent about battery usage. LiteP2P's efficient design means battery impact is minimal when properly configured.