What is a background task — and why the phone puts it to sleep anyway

"Let the app check for new messages once a minute" sounds like a ten-line job. An AI assistant will write that code in a second. On a phone it will run for about thirty seconds and then go quiet forever.
It's not a bug. It's that background on a phone isn't work, it's a freeze. The sooner you accept that, the fewer evenings you'll spend debugging something that isn't fixed in code.
What a background task is, in plain words
A background task is work your app does while its screen isn't open: finish syncing, download a file, refresh the feed before morning.
The key difference from a computer: on a phone you don't start background work, you beg for it. The moment someone leaves your app, the system has every right to freeze the whole process to save battery. Not kill it — pause it: code stops, timers don't tick, the network doesn't answer.
So a "background task" isn't a separate thread inside your program. It's a contract with the operating system: you state what you'd like to do, and it decides when — or whether — to let you.
How much time you actually get
The numbers are sobering.
On iOS, if your app goes to the background and asks for a little time to finish what it started, expect roughly 30 seconds. Don't finish and don't end the task, and the watchdog kills the app. And no, the trick of "starting two tasks" doesn't work: the time belongs to the app, not to the tasks, so two thirties don't add up.
On Android, a phone lying still for a while enters Doze. The docs list what happens without hedging: the system suspends network access, defers scheduler jobs, defers standard alarms to the next maintenance window, ignores wake locks, doesn't run Wi-Fi scans, and doesn't let sync adapters run. Periodically the device briefly leaves Doze — that's the maintenance window, where everything queued runs at once. And one detail that's easy to miss: over time the system schedules those windows less and less often.
Three honest ways to work in the background
Since spinning a loop is out, three approaches remain — pick by who's waiting for the result.
- Deferred work with conditions. You don't say "in 60 seconds," you say "when there's a network" or "when it's charging." The system batches such jobs into shared windows — which saves battery and survives a reboot.
- Work the user can see. A music player, a workout recording, a large upload. Here the OS genuinely lets you run, but demands a persistent notification in the status bar: people must see that your app is doing something with their resources.
- A server instead of polling. The most underrated option. Rather than waking the phone to ask "any news?", let the server send a push when there is news. One notification instead of a thousand empty checks.
What the phone will definitely not allow
Things that look sensible in code and don't work on a device:
- An infinite loop with a sleep. App backgrounded, loop frozen at the first sleep. It isn't running slowly — it's stopped.
- A permanently open connection to your server. In the background it gets torn down, and reconnect attempts burn battery until the system restricts your app.
- Exact timing. "Send the report at exactly 9:00" isn't guaranteed on a phone: in Doze the alarm is deferred to the maintenance window.
- Work right after a reboot. Until someone opens the app at least once, plenty of things simply won't start.
If any of those is the foundation of your idea, the idea moves to a server. That's not defeat, it's a normal division of labour: the phone shows, the server remembers and counts.
How this changes your architecture
Three habits that save weeks:
- Think in events, not schedules. "Every minute" is a wish. "When there's a network and something to send" is what the phone will actually do.
- Save results immediately. A task can be interrupted halfway. Anything not written to the local database is lost — more on that in the offline guide.
- Make the task repeatable. The system has no problem running your work twice. If the second run creates a second order, that's on the code, not the OS.
Why does it work in the emulator but not on a real phone?
Because an emulator is usually plugged in, screen on, power saving off — Doze never kicks in. Test on a real device, off the cable, and not after a minute but after an hour of sitting still. Half of all "flaky" background bugs surface exactly that way.
Can I ask to be exempt from battery optimisation?
Technically yes, and sometimes it's legitimate — an alarm clock, say. But it's a poor first move: the user sees a scary system prompt, and app stores expect you to justify it. Restructure the logic first; keep the exemption for later.
How is this different from a cron job on a server?
Guarantees. Server cron fires at the stated time because the server doesn't sleep. A background task on a phone is a request the system fulfils when it sees fit: maybe in ten minutes, maybe in the morning while charging. If you need precision, move the work to a server.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





