theme: apple-basic
layout: intro
highlighter: shiki
lineNumbers: true
실행 시점에 따른 분류
실행 완료 여부에 따른 분류
| Exact Timing | Deferrable | |
|---|---|---|
| Best-Effort | ThreadPool | ThreadPool |
| Guaranteed Execution | ForegroundService | JobScheduler, JobDispatcher, AlarmManager, BroadcastReceivers |
AlarmManager와 JobScheduler를 알아서 선택
| Use Case | Examples | Solution |
|---|---|---|
| Guaranteed execution of deferrable work | Upload logs to your server, Encrypt/Decrypt content to upload/download | WorkManager |
| A task initiated in response to an external event | Syncing new online content like email | FCM + WorkManager |
| Continue user-initiated work that needs to run immediately even if the user leaves the app | Music player, Tracking activity, Transit navigation | Foreground Service |
| Trigger actions that involve user interactions, like notifications at an exact time. | Alarm clock, Medicine reminder, Notification about a TV show that is about to start | AlarmManager |
class UploadWorker(appContext: Context, workerParams: WorkerParameters):
Worker(appContext, workerParams) {
override fun doWork(): Result {
// Do the work here--in this case, upload the images.
uploadImages()
// Indicate whether the work finished successfully with the Result
return Result.success()
}
}
| Constraints | Description |
|---|---|
| NetworkType | Constrains the type of network required for your work to run. For example, Wi-Fi (UNMETERED). |
| BatteryNotLow | When set to true, your work will not run if the device is in low battery mode. |
| RequiresCharging | When set to true, your work will only run when the device is charging. |
| DeviceIdle | When set to true, this requires the user’s device to be idle before the work will run. This can be useful for running batched operations that might otherwise have a negative performance impact on other apps running actively on the user’s device. |
| StorageNotLow | When set to true, your work will not run if the user’s storage space on the device is too low. |
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresCharging(true)
.build()
val uploadWorkRequest: WorkRequest =
OneTimeWorkRequestBuilder<UploadWorker>()
.setConstraints(constraints)
.build()
WorkManager
.getInstance(myContext)
.enqueue(uploadWorkRequest)
// by id
workManager.getWorkInfoById(syncWorker.id) // ListenableFuture<WorkInfo>
// by name
workManager.getWorkInfosForUniqueWork("sync") // ListenableFuture<List<WorkInfo>>
// by tag
workManager.getWorkInfosByTag("syncTag") // ListenableFuture<List<WorkInfo>>
1회성 작업
주기적 작업