Implement reliable background processing in Android using WorkManager, foreground services, and proper battery optimization handling.
You are an Android background processing expert. Help me implement background tasks that work reliably across all Android versions.
Task Requirements:
- Task Type: {{task_type}} (sync, upload, download, periodic, location)
- Frequency: {{frequency}} (one-time/periodic/exact)
- Network Required: {{network_required}}
- Duration: {{task_duration}}
- Critical: {{is_critical}}
Android Support:
- Minimum SDK: {{min_sdk}}
- Target SDK: {{target_sdk}}
Please provide implementation for:
1. WorkManager Implementation:
- Worker class implementation
- Work request configuration
- Constraints setup (network, battery, storage)
- Chaining and parallel work
- Unique work and conflict resolution
- Periodic work with flex interval
2. Foreground Services:
- Foreground service types
- Notification requirements
- Service lifecycle
- Location/media foreground services
- Stopping gracefully
3. Coroutines Integration:
- CoroutineWorker implementation
- Progress reporting
- Cancellation handling
- Error handling and retry
4. Battery Optimization:
- Doze mode handling
- App Standby buckets
- Battery optimization exemption requests
- Expedited work for critical tasks
5. Data Sync:
- ContentProvider integration
- SyncAdapter (legacy)
- Push-triggered sync
- Conflict resolution
6. Scheduling Strategies:
- AlarmManager usage (when necessary)
- Exact alarms (Android 12+)
- Boot persistence
- App update persistence
7. Testing:
- WorkManager testing
- Service testing
- Background restriction testingOr press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
{{task_type}{{frequency}{{network_required}{{task_duration}{{is_critical}{{min_sdk}{{target_sdk}