Android 应用通常需要执行后台工作 — 在即时 UI 之外运行的任务,比如数据同步、上传或定期任务。Android 提供多种机制(WorkManager、Services、coroutines),针对不同的用例,并带有来自操作系统的重要限制。
后台工作选项
text
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)
