Your mobile agent needs to survive phone locks and network drops
Your AI agent dies the moment your phone screen locks. Or when you switch from WiFi to cellular. Or when you walk into an elevator.
This is the dirty secret of mobile AI agents that nobody talks about. They're built like desktop apps — they assume persistent connections, foreground execution, and unlimited battery. But phones are hostile environments.
Here's what actually works: background-first architecture. Design your agent like a server process, not a chat app.
Real example: I run a Gemini agent on my Android that monitors my calendar, sends proactive reminders, and handles routine emails. It's been running for 3 weeks straight through phone locks, airplane mode, and battery optimization.
The key is treating your phone like a cloud server with terrible uptime. Here's the pattern:
- Persistent state — Store everything in local SQLite, not memory
- Resumable tasks — Every operation can restart from where it left off
- Offline queuing — Queue actions when disconnected, execute when online
- Heartbeat scheduling — Use system alarms, not while loops
Here's the Android WorkManager config that actually survives:
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(false)
.setRequiresStorageNotLow(false)
.build()
val agentWork = PeriodicWorkRequestBuilder<AgentWorker>(15, TimeUnit.MINUTES)
.setConstraints(constraints)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS)
.build()The 15-minute interval is crucial — Android kills anything more frequent. The exponential backoff prevents your agent from hammering dead APIs.
For the actual agent logic, think in atomic operations:
class AgentWorker : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
return try {
val pendingTasks = database.getPendingTasks()
pendingTasks.forEach { task ->
when (task.type) {
"email_check" -> processEmails()
"calendar_sync" -> syncCalendar()
"reminder_send" -> sendReminders()
}
database.markCompleted(task.id)
}
Result.success()
} catch (e: Exception) {
Result.retry()
}
}
}Each task is independent. If one fails, the others still run. If the whole worker dies, it picks up where it left off.
Warning: Don't use Firebase Cloud Messaging for agent communication. FCM has delivery delays and rate limits. Use direct API polling with exponential backoff instead.
The breakthrough insight: your phone agent should work like email. It syncs when it can, queues when it can't, and never loses data. Stop building real-time chat agents for an environment that kills background processes.
Battery optimization is your enemy. Most users will never whitelist your app, so design around aggressive power management. Use the minimum viable frequency, batch operations, and fail gracefully.
This pattern works for any mobile agent — monitoring, automation, or assistance. The key is accepting that mobile is fundamentally unreliable and building resilience from day one.