一键导入
android-background-execution
Expert guidance on implementing and managing background tasks in modern Android applications (API 30+).
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Expert guidance on implementing and managing background tasks in modern Android applications (API 30+).
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | android-background-execution |
| description | Expert guidance on implementing and managing background tasks in modern Android applications (API 30+). |
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).
Use for persistent, deferrable work that must execute even if the app exits or the device restarts.
setExpedited(true)).Use for immediate, user-visible work that is the primary focus of the app (e.g., Music Player, Step Tracker).
foregroundServiceType in the manifest.AlarmManager)Use for exact, time-based scheduling.
setExactAndAllowWhileIdle() is for high-precision needs (e.g., clocks).| 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 |
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)
<service
android:name=".MyService"
android:foregroundServiceType="dataSync" />
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
}
}
WorkManager handles this automatically by batching requests.setExpedited(true). Note that this has quotas.Constraints in WorkManager to prevent unnecessary wakeups when offline.WorkManagerTestInitHelper for testing workers in unit/integration tests without waiting for real-world constraints.com.example.app.data.worker or com.example.app.ui.background.com.example.app.service.Repository classes, not inside Worker or Service classes.Handling Bluetooth Low Energy, flow wrappers, and Android 12+ permissions.
Seamless integration of CameraX with Google ML Kit for vision tasks.
Deep-dive skills for banking and healthcare apps.
Use when integrating Google Gemini Nano via AICore for on-device ML.
Automated UIAutomator tests and Hardware mocking setup.
Expanding KMP shared UI to cover Compose Multiplatform edge-cases.