Battery life
What drains a phone's battery, the limits the system puts on background work, the wake lock, batching and deferring tasks, and choosing the accuracy and frequency of location updates.
- 29 cards
- 7 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- On the timeline
- The numbers
- Scenarios
- Understand
A few cards from the deck
Battery consumption: what is it?
Show the answer
The energy an app makes the phone spend, mostly through the screen, the radio (network, GPS) and a processor kept awake.
An app costs little when it does nothing; what weighs is what it wakes up: a network send, a GPS fix, a computation that keeps the processor out of sleep.
What is wrong with this code?
```kotlin
val lock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "app:sync")
lock.acquire()
syncAll()`- The lock is never released:
release()is missing, even after a successful sync PARTIAL_WAKE_LOCKturns the screen on during the sync- A wake lock can only be taken from the foreground activity
- The name
"app:sync"is reserved for the system
Show the answer
The lock is never released:
release()is missing, even after a successful syncThe lock is acquired but never released: the processor stays awake with the screen off. Use
try { syncAll() } finally { lock.release() }, so that an exception cannot skip it either, andacquire(10 * 60 * 1000L)so it expires by itself.PARTIAL_WAKE_LOCKkeeps only the processor on, not the screen.- The lock is never released:
A wake lock must always be … once the job is done, in a … block, so that an exception does not leave it held.
Show the answer
A wake lock must always be released once the job is done, in a finally block, so that an exception does not leave it held.
Without
release(), the processor stays awake with the screen off until the process dies.acquire(timeout)adds a safety net: the lock releases itself.Background work: what is it?
Show the answer
A task the app runs with no visible screen: sync, upload, reminder, data refresh.
The system limits this work to protect the battery: an app cannot assume it will run at the exact moment it wants while its screen is closed.