Doing things when not in front.
WHY IT IS RESTRICTED
Background work consumes battery, and the platform has progressively restricted it.
WHAT THE RESTRICTIONS MEAN
You cannot simply run whatever you want, whenever you want.
WHAT TO USE FOR DEFERRABLE WORK
The scheduling library, which respects system constraints and survives restarts.
WHAT CONSTRAINTS TO DECLARE
Requires a network Requires charging Requires sufficient battery
WHY
The system then chooses an appropriate time.
WHAT TO USE FOR WORK THE USER IS AWARE OF
A foreground service, with a visible notification.
WHEN THAT IS APPROPRIATE
Playing media, navigation, active uploads, recording.
WHAT IT REQUIRES
Declaring the purpose, and justifying it at review.
WHAT TO USE FOR EXACT TIMING
The alarm facilities, sparingly, since they are restricted.
WHAT TO NEVER DO
Poll frequently in the background Hold wake locks longer than necessary Perform network requests on every application start unnecessarily
WHY
Battery consumption is visible to users, and attributed to your application.
WHAT TO TEST
Behaviour under battery optimisation, which is enabled by default.