home / skills / hoangnguyen0403 / agent-skills-standard / background-work

background-work skill

/skills/android/background-work

This skill helps you implement robust Android background tasks using WorkManager, explicit constraints, Hilt integration, and foreground service guidelines for

npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill background-work

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
949 B
---
name: Android Background Work
description: Standards for WorkManager and Background Processing
metadata:
  labels: [android, background, workmanager]
  triggers:
    files: ['**/*Worker.kt']
    keywords: ['CoroutineWorker', 'WorkManager', 'doWork']
---

# Android Background Work Standards

## **Priority: P1**

## Implementation Guidelines

### WorkManager

- **CoroutineWorker**: Use for all background tasks.
- **Constraints**: Be explicit (Require Network, Charging).
- **Hilt**: Use `@HiltWorker` for DI integration.

### Foreground Services

- **Only When Necessary**: Use generating visible notifications only for tasks the user is actively aware of (Playback, Calls, Active Navigation). Otherwise use WorkManager.

## Anti-Patterns

- **IntentService**: `**Deprecated**: Use WorkManager.`
- **Short Jobs**: `**No short background jobs**: Use standard Coroutines in VM.`

## References

- [Worker Template](references/implementation.md)

Overview

This skill codifies standards for Android background work, focusing on WorkManager and safe foreground service usage. It prescribes CoroutineWorker, explicit constraints, and Hilt integration to ensure reliable, testable, and battery-friendly background processing. The guidance also highlights common anti-patterns to avoid and when to prefer in-process coroutines or foreground services.

How this skill works

The skill inspects background task definitions and recommends using WorkManager with CoroutineWorker as the default implementation for scheduled, deferrable, and guaranteed work. It enforces explicit constraints (network, charging, battery) and suggests using @HiltWorker for dependency injection. For user-visible continuous tasks, it recommends foreground services with visible notifications; for short-lived or UI-scoped work, it advises using coroutines in the ViewModel instead of background workers.

When to use it

  • Use WorkManager (CoroutineWorker) for deferrable, guaranteed background tasks like uploads, sync, or retries.
  • Apply explicit Constraints when a job needs network, charging, or storage availability.
  • Use @HiltWorker to inject repositories, data sources, and services into workers.
  • Prefer a foreground service only for user-visible, long-running tasks (playback, calls, active navigation).
  • Run short or immediate tasks inside ViewModel coroutines rather than scheduling a Worker.

Best practices

  • Default to CoroutineWorker for all WorkManager jobs to leverage structured concurrency and cancellation.
  • Declare constraints explicitly to avoid unintended battery or data usage.
  • Annotate workers with @HiltWorker and use AssistedInjection patterns for runtime params.
  • Avoid IntentService and other deprecated APIs; migrate legacy code to WorkManager.
  • Do not schedule trivial short-lived tasks with WorkManager—use local coroutines instead.

Example use cases

  • Periodic background data sync that requires network and charging: use WorkManager with Constraints(requireNetwork = true, requireCharging = true).
  • Reliable upload retry after intermittent connectivity: CoroutineWorker with backoff policy.
  • Dependency-injected worker that needs a repository: @HiltWorker with AssistedInject to pass WorkParameters.
  • Music playback or turn-by-turn navigation: run as foreground service with a visible notification.
  • One-off UI-triggered short job (save draft, format text): perform in ViewModel coroutine without scheduling a Worker.

FAQ

When should I use a foreground service instead of WorkManager?

Use a foreground service only for long-running tasks the user expects to see (media playback, active navigation, phone calls). For background work that does not require immediate user awareness, prefer WorkManager.

Can I still use IntentService or JobIntentService?

No. Those APIs are deprecated for most background work. Migrate to WorkManager and CoroutineWorker for reliability and modern lifecycle handling.