Androidアプリでは、データの同期、アップロード、定期的なジョブなど、即時のUIの外で実行される バックグラウンド処理 が必要になることがよくあります。Androidには、それぞれ異なるユースケースを持つ複数の仕組み(WorkManager、Service、コルーチン)に加えて、OSからの重要な制約があります。
バックグラウンド処理の選択肢
COROUTINES (in a ViewModel scope) → for async work tied to the UI/screen (network calls,
loading data) — runs while the screen is active
WORKMANAGER → for DEFERRABLE, GUARANTEED background work that must complete (even if the
app closes or the device reboots): syncing, uploads, periodic tasks, retries
→ the RECOMMENDED solution for most persistent background work
FOREGROUND SERVICE → for ongoing, user-visible background work (music playback, navigation,
active tracking) — shows a persistent notification
SERVICES → general background components (largely superseded by WorkManager/coroutines
for most cases)
