Android ਐਪਸ ਨੂੰ ਅਕਸਰ background work ਕਰਨਾ ਪੈਂਦਾ ਹੈ — ਅਜਿਹੇ ਕੰਮ ਜੋ ਤੁਰੰਤ UI ਤੋਂ ਬਾਹਰ ਚਲਦੇ ਹਨ, ਜਿਵੇਂ data sync ਕਰਨਾ, uploads ਕਰਨਾ, ਜਾਂ periodic jobs। Android ਕਈ mechanisms (WorkManager, Services, coroutines) ਪ੍ਰਦਾਨ ਕਰਦਾ ਹੈ ਜਿਨ੍ਹਾਂ ਦੇ ਵੱਖ-ਵੱਖ use cases ਹਨ, ਨਾਲ ਹੀ OS ਤੋਂ ਮਹੱਤਵਪੂਰਨ constraints ਹਨ।
Background work options
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)
