android-background-execution
Skill11 starsChanged 8 months agoMITView on GitHub
Expert guidance on implementing and managing background tasks in modern Android applications (API 30+).
In this repository40 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- android-accessibilitySkill
- android-adaptive-layoutsSkill
- android-adb-terminalSkill
- android-bluetooth-bleSkill
- android-build-verificationSkill
- android-camerax-mlkitSkill
- android-ci-cd-expertSkill
- android-clean-architectureSkill
- android-code-review-expertSkill
- android-coroutines-expertSkill
- android-custom-drawing-canvasSkill
- android-design-system-m3Skill
- android-design-to-codeSkill
- android-detekt-expertSkill
- android-enterprise-securitySkill
- android-gemini-nanoSkill
- android-google-workspace-expertSkill
- android-gradle-expertSkill
- android-hardware-testingSkill
- android-jetpack-composeSkill
- android-kmp-compose-uiSkill
- android-kmp-platform-channelsSkill
- android-kmp-sharedSkill
- android-ktlint-formattingSkill
- android-lint-and-qualitySkill
- android-local-stt-ttsSkill
- android-location-mapsSkill
- android-macrobenchmarkSkill
- android-media3-exoplayerSkill
- android-media-and-image-expertSkill
- android-motion-animationsSkill
- android-navigation-expertSkill
- android-offline-firstSkill
- android-performance-auditSkill
- android-rag-localSkill
- android-screenshot-testingSkill
- android-security-hardenedSkill
- android-unit-testingSkill
- android-wear-os-composeSkill
The file
We split this skill into the rules it gives an agent
10 rules. About 0 are common, 5 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 21
Use for persistent, deferrable work that must execute even if the app exits or the device restarts.
- Distinctive: line 29
Use for immediate, user-visible work that is the primary focus of the app (e.g., Music Player, Step Tracker).
- Distinctive: line 31
Must show a persistent notification.
- Distinctive: line 36
Use for exact, time-based scheduling.
- Distinctive: line 38
Avoid for general background work to save battery.
- Very specific: line 120
Connectivity: Always use Constraints in WorkManager to prevent unnecessary wakeups when offline.
- Very specific: line 122
Testing: Use WorkManagerTestInitHelper for testing workers in unit/integration tests without waiting for real-world constraints.
- Very specific: line 126
Place Workers in com.example.app.data.worker or com.example.app.ui.background.
- Very specific: line 127
Place Services in com.example.app.service.
- Very specific: line 128
Keep business logic in Repository classes, not inside Worker or Service classes.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Android Background Execution Skill
This skill provides a structured approach to implementing background work in Android, ensuring high performance, battery efficiency, and compliance with modern system restrictions (API 30 and above).
When to Use
- Implementing deferred tasks (e.g., database sync, log uploading).
- Running immediate, user-perceivable tasks (e.g., media playback, active navigation).
- Scheduling time-sensitive actions (e.g., calendar reminders, exact alarms).
- Navigating background execution limits and battery optimization (Doze Mode).
🛠 Core Technologies
1. WorkManager (Recommended)
Use for persistent, deferrable work that must execute even if the app exits or the device restarts.
- Immediate: Tasks that should start as soon as possible (can use
setExpedited(true)). - Long-Running: Tasks that might run longer than 10 minutes (API 31+ requires a Foreground Service).
- Periodic: Recurring tasks (minimum 15-minute interval).
2. Foreground Services
Use for immediate, user-visible work that is the primary focus of the app (e.g., Music Player, Step Tracker).
- Must show a persistent notification.
- From API 34+, you must declare a
foregroundServiceTypein the manifest.
3. Alarms (AlarmManager)
Use for exact, time-based scheduling.
- Avoid for general background work to save battery.
setExactAndAllowWhileIdle()is for high-precision needs (e.g., clocks).
🚀 Implementation Workflow
1. Choose the Right Tool
| Requirement | Recommended Tool |
| Deferrable, restart-resilient | WorkManager (OneTime/Periodic) |
| Immediate, continues when app is closed | WorkManager (Expedited) |
| User-visible, real-time | Foreground Service |
| Specific clock time (e.g. 8:00 AM) | AlarmManager |
2. Implementation Patterns
WorkManager (Kotlin)
class SyncWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
return try {
// Background logic here
Result.success()
} catch (e: Exception) {
if (runAttemptCount < 3) Result.retry() else Result.failure()
}
}
}// Enqueueing
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)
.build()
val syncRequest = OneTimeWorkRequestBuilder<SyncWorker>()
.setConstraints(constraints)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build()
WorkManager.getInstance(context).enqueue(syncRequest)Foreground Service (API 34+)
- Manifest Declaration:
<service
android:name=".MyService"
android:foregroundServiceType="dataSync" />- Service Implementation:
class MyService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Syncing Data")
.setSmallIcon(R.drawable.ic_sync)
.build()
ServiceCompat.startForeground(this, 1, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC)
// logic...
return START_NOT_STICKY
}
}💡 Best Practices & Constraints
- Battery Optimization: Respect Doze Mode.
WorkManagerhandles this automatically by batching requests. - Expedited Work: For WorkManager tasks that need to start immediately, use
setExpedited(true). Note that this has quotas. - Connectivity: Always use
ConstraintsinWorkManagerto prevent unnecessary wakeups when offline. - Error Handling: Implement exponential backoff for retries to avoid "looping" failures.
- Testing: Use
WorkManagerTestInitHelperfor testing workers in unit/integration tests without waiting for real-world constraints.
📂 Project Organization
- Place Workers in
com.example.app.data.workerorcom.example.app.ui.background. - Place Services in
com.example.app.service. - Keep business logic in
Repositoryclasses, not insideWorkerorServiceclasses.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

