#1 이전 글
위 게시글의 완성된 앱을 일부 수정해서, WorkRequest의 실행에 필요한 조건을 설정해본다.
작업마다 서로 다른 환경에서 수행되기를 바랄 수 있다. 해당 환경을 나타내는 제약 조건(constraint)를 프로그래머가 명시적인 객체로서 정의할 수 있다. 예를 들어 대용량 파일을 서버에 전송하는 경우, 인터넷 연결과 배터리가 충전되고 있는 상태가 제약 조건으로서 필요할 것이다. 아래에 있는 코드를 보자.
#2 코드 수정
#2-1 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
...
</application>
</manifest>
본 게시글에서 사용할 제약 조건 중 인터넷 연결을 확인하는 코드가 있으므로, 위와 같이 해당 동작에 대한 권한을 등록한다.
#2-2 MainActivity.kt
// package com.example.constraint
...
import androidx.work.Constraints
import androidx.work.NetworkType
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
}
private fun setOneTimeWorkRequest() {
// 제약 조건 객체 만들기
val constraints = Constraints
.Builder()
.setRequiresCharging(true)
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
// WorkRequest 객체 만들기
val uploadRequest = OneTimeWorkRequest
.Builder(UploadWorker::class.java)
.setConstraints(constraints)
.build()
// WorkManager 객체의 작업 큐(Job queue)에 WorkRequest 등록
...
// WorkInfo는 작업에 대한 정보를 담은 객체.
// WorkManager.getWorkInfoByIdLiveData()는 LiveData<WorkInfo>를 반환.
...
}
private fun setPeriodicWorkRequest() {
// 제약 조건 객체 만들기
val constraints = Constraints
.Builder()
.setRequiresCharging(true)
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
// WorkRequest 객체 만들기
val downloadRequest = PeriodicWorkRequest
.Builder(DownloadWorker::class.java, 15, TimeUnit.MINUTES) // 안드로이드에서 허용하는 PeriodicWorkRequest의 최소 주기는 15분이다.
.setConstraints(constraints)
.build()
// WorkManager 객체의 작업 큐(Job queue)에 WorkRequest 등록
...
// WorkInfo는 작업에 대한 정보를 담은 객체.
// WorkManager.getWorkInfoByIdLiveData()는 LiveData<WorkInfo>를 반환.
...
}
}
Constraints 객체를 WorkRequest 객체에 넣는 방식이다.
#3 작동 확인
#3-1 안드로이드 스튜디오에서 에뮬레이터의 환경을 설정하는 방법
'Extended Controls' 버튼을 누르면,
위와 같이 여러가지 설정을 바꿀 수 있다. #3-2에서는 Cellular 설정을, #3-3에서는 Battery 설정을 건들여본다.
#3-2 'One Time Request' 버튼을 클릭했을 때의 로그 메시지
2024-06-17 18:05:14.642 (MainActivity) UploadWorker's state: ENQUEUED
2024-06-17 18:16:04.288 (UploadWorker) Started at 2024/06/17 09:16:04
2024-06-17 18:16:04.288 (UploadWorker) Progress: 0/100
2024-06-17 18:16:04.288 (UploadWorker) Progress: 1/100
2024-06-17 18:16:04.288 (UploadWorker) Progress: 2/100
2024-06-17 18:16:04.288 (UploadWorker) Progress: 3/100
2024-06-17 18:16:04.288 (UploadWorker) Progress: 4/100
2024-06-17 18:16:04.288 (UploadWorker) Progress: 5/100
2024-06-17 18:16:04.288 (UploadWorker) Progress: 6/100
2024-06-17 18:16:04.288 (UploadWorker) Progress: 7/100
2024-06-17 18:16:04.293 (UploadWorker) Progress: 8/100
2024-06-17 18:16:04.294 (UploadWorker) Progress: 9/100
2024-06-17 18:16:04.295 (UploadWorker) Progress: 10/100
2024-06-17 18:16:04.295 (UploadWorker) Progress: 11/100
2024-06-17 18:16:04.299 (UploadWorker) Progress: 12/100
2024-06-17 18:16:04.299 (UploadWorker) Progress: 13/100
2024-06-17 18:16:04.299 (UploadWorker) Progress: 14/100
2024-06-17 18:16:04.299 (UploadWorker) Progress: 15/100
2024-06-17 18:16:04.299 (UploadWorker) Progress: 16/100
2024-06-17 18:16:04.299 (UploadWorker) Progress: 17/100
2024-06-17 18:16:04.299 (UploadWorker) Progress: 18/100
2024-06-17 18:16:04.299 (UploadWorker) Progress: 19/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 20/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 21/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 22/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 23/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 24/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 25/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 26/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 27/100
2024-06-17 18:16:04.300 (UploadWorker) Progress: 28/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 29/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 30/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 31/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 32/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 33/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 34/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 35/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 36/100
2024-06-17 18:16:04.301 (UploadWorker) Progress: 37/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 38/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 39/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 40/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 41/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 42/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 43/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 44/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 45/100
2024-06-17 18:16:04.302 (UploadWorker) Progress: 46/100
2024-06-17 18:16:04.303 (UploadWorker) Progress: 47/100
2024-06-17 18:16:04.303 (UploadWorker) Progress: 48/100
2024-06-17 18:16:04.303 (UploadWorker) Progress: 49/100
2024-06-17 18:16:04.303 (UploadWorker) Progress: 50/100
2024-06-17 18:16:04.303 (UploadWorker) Progress: 51/100
2024-06-17 18:16:04.303 (UploadWorker) Progress: 52/100
2024-06-17 18:16:04.303 (UploadWorker) Progress: 53/100
2024-06-17 18:16:04.304 (UploadWorker) Progress: 54/100
2024-06-17 18:16:04.304 (UploadWorker) Progress: 55/100
2024-06-17 18:16:04.304 (UploadWorker) Progress: 56/100
2024-06-17 18:16:04.304 (UploadWorker) Progress: 57/100
2024-06-17 18:16:04.304 (UploadWorker) Progress: 58/100
2024-06-17 18:16:04.305 (UploadWorker) Progress: 59/100
2024-06-17 18:16:04.305 (UploadWorker) Progress: 60/100
2024-06-17 18:16:04.305 (UploadWorker) Progress: 61/100
2024-06-17 18:16:04.305 (UploadWorker) Progress: 62/100
2024-06-17 18:16:04.305 (UploadWorker) Progress: 63/100
2024-06-17 18:16:04.305 (UploadWorker) Progress: 64/100
2024-06-17 18:16:04.305 (UploadWorker) Progress: 65/100
2024-06-17 18:16:04.305 (UploadWorker) Progress: 66/100
2024-06-17 18:16:04.306 (UploadWorker) Progress: 67/100
2024-06-17 18:16:04.306 (UploadWorker) Progress: 68/100
2024-06-17 18:16:04.306 (UploadWorker) Progress: 69/100
2024-06-17 18:16:04.306 (UploadWorker) Progress: 70/100
2024-06-17 18:16:04.306 (UploadWorker) Progress: 71/100
2024-06-17 18:16:04.306 (UploadWorker) Progress: 72/100
2024-06-17 18:16:04.306 (UploadWorker) Progress: 73/100
2024-06-17 18:16:04.306 (UploadWorker) Progress: 74/100
2024-06-17 18:16:04.307 (UploadWorker) Progress: 75/100
2024-06-17 18:16:04.307 (UploadWorker) Progress: 76/100
2024-06-17 18:16:04.307 (UploadWorker) Progress: 77/100
2024-06-17 18:16:04.307 (UploadWorker) Progress: 78/100
2024-06-17 18:16:04.307 (UploadWorker) Progress: 79/100
2024-06-17 18:16:04.307 (UploadWorker) Progress: 80/100
2024-06-17 18:16:04.307 (UploadWorker) Progress: 81/100
2024-06-17 18:16:04.307 (UploadWorker) Progress: 82/100
2024-06-17 18:16:04.308 (UploadWorker) Progress: 83/100
2024-06-17 18:16:04.308 (UploadWorker) Progress: 84/100
2024-06-17 18:16:04.308 (UploadWorker) Progress: 85/100
2024-06-17 18:16:04.308 (UploadWorker) Progress: 86/100
2024-06-17 18:16:04.308 (UploadWorker) Progress: 87/100
2024-06-17 18:16:04.308 (UploadWorker) Progress: 88/100
2024-06-17 18:16:04.308 (UploadWorker) Progress: 89/100
2024-06-17 18:16:04.309 (UploadWorker) Progress: 90/100
2024-06-17 18:16:04.309 (UploadWorker) Progress: 91/100
2024-06-17 18:16:04.309 (UploadWorker) Progress: 92/100
2024-06-17 18:16:04.309 (UploadWorker) Progress: 93/100
2024-06-17 18:16:04.309 (UploadWorker) Progress: 94/100
2024-06-17 18:16:04.309 (UploadWorker) Progress: 95/100
2024-06-17 18:16:04.309 (UploadWorker) Progress: 96/100
2024-06-17 18:16:04.309 (UploadWorker) Progress: 97/100
2024-06-17 18:16:04.310 (UploadWorker) Progress: 98/100
2024-06-17 18:16:04.310 (UploadWorker) Progress: 99/100
2024-06-17 18:16:04.310 (UploadWorker) Progress: 100/100
2024-06-17 18:16:04.346 (UploadWorker) Finished at 2024/06/17 09:16:04
2024-06-17 18:16:04.402 (MainActivity) UploadWorker's state: SUCCEEDED
Extended Controls - Cellular - Data status를 Denied로 바꾸고, 에뮬레이터 상태바를 내려서 Wifi 연결도 잊지 말고 끊는다. 'One Time Request' 버튼을 누르고 약 10분 정도 대기했다. 그리고 Data status를 Home으로 바꿨을 때의 로그 메시지다.
#3-3 'Periodic Request' 버튼을 클릭했을 때의 로그 메시지
2024-06-17 18:19:39.020 (MainActivity) DownloadWorker's state: ENQUEUED
2024-06-17 18:30:36.043 (DownloadWorker) Started at 2024/06/17 09:30:36
2024-06-17 18:30:36.043 (DownloadWorker) Progress: 0/100
2024-06-17 18:30:36.044 (DownloadWorker) Progress: 1/100
2024-06-17 18:30:36.044 (DownloadWorker) Progress: 2/100
2024-06-17 18:30:36.044 (DownloadWorker) Progress: 3/100
2024-06-17 18:30:36.044 (DownloadWorker) Progress: 4/100
2024-06-17 18:30:36.044 (DownloadWorker) Progress: 5/100
2024-06-17 18:30:36.044 (DownloadWorker) Progress: 6/100
2024-06-17 18:30:36.044 (DownloadWorker) Progress: 7/100
2024-06-17 18:30:36.044 (DownloadWorker) Progress: 8/100
2024-06-17 18:30:36.045 (DownloadWorker) Progress: 9/100
2024-06-17 18:30:36.045 (DownloadWorker) Progress: 10/100
2024-06-17 18:30:36.045 (DownloadWorker) Progress: 11/100
2024-06-17 18:30:36.045 (DownloadWorker) Progress: 12/100
2024-06-17 18:30:36.045 (DownloadWorker) Progress: 13/100
2024-06-17 18:30:36.051 (DownloadWorker) Progress: 14/100
2024-06-17 18:30:36.051 (DownloadWorker) Progress: 15/100
2024-06-17 18:30:36.051 (DownloadWorker) Progress: 16/100
2024-06-17 18:30:36.051 (DownloadWorker) Progress: 17/100
2024-06-17 18:30:36.051 (DownloadWorker) Progress: 18/100
2024-06-17 18:30:36.051 (DownloadWorker) Progress: 19/100
2024-06-17 18:30:36.051 (DownloadWorker) Progress: 20/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 21/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 22/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 23/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 24/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 25/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 26/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 27/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 28/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 29/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 30/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 31/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 32/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 33/100
2024-06-17 18:30:36.052 (DownloadWorker) Progress: 34/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 35/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 36/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 37/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 38/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 39/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 40/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 41/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 42/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 43/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 44/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 45/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 46/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 47/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 48/100
2024-06-17 18:30:36.053 (DownloadWorker) Progress: 49/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 50/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 51/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 52/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 53/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 54/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 55/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 56/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 57/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 58/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 59/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 60/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 61/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 62/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 63/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 64/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 65/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 66/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 67/100
2024-06-17 18:30:36.054 (DownloadWorker) Progress: 68/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 69/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 70/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 71/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 72/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 73/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 74/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 75/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 76/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 77/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 78/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 79/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 80/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 81/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 82/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 83/100
2024-06-17 18:30:36.055 (DownloadWorker) Progress: 84/100
2024-06-17 18:30:36.056 (DownloadWorker) Progress: 85/100
2024-06-17 18:30:36.056 (DownloadWorker) Progress: 86/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 87/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 88/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 89/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 90/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 91/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 92/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 93/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 94/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 95/100
2024-06-17 18:30:36.057 (DownloadWorker) Progress: 96/100
2024-06-17 18:30:36.058 (DownloadWorker) Progress: 97/100
2024-06-17 18:30:36.058 (DownloadWorker) Progress: 98/100
2024-06-17 18:30:36.058 (DownloadWorker) Progress: 99/100
2024-06-17 18:30:36.058 (DownloadWorker) Progress: 100/100
2024-06-17 18:30:36.059 (DownloadWorker) Finished at 2024/06/17 09:30:36
2024-06-17 18:30:36.152 (MainActivity) DownloadWorker's state: ENQUEUED
이하 생략
Extended Controls - Charger connection을 None으로, Extended Controls - Battery status를 Not charging으로 바꾼다. 'Periodic Request' 버튼을 누르고 약 10분 정도 대기했다. 그리고 Charger connection을 AC charger로, Battery status를 Charging으로 바꿨을 때의 로그 메시지다.
#4 요약
Constraints는, WorkManager의 '배달 주의 사항'과 같다.
#5 완성된 앱
'깨알 개념 > Android' 카테고리의 다른 글
[Android] WorkManager - 병렬 Chaining (0) | 2024.06.17 |
---|---|
[Android] WorkManager - 작업 연쇄하기(Chaining) (0) | 2024.06.17 |
[Android] WorkManager - 인자 전달 및 데이터 반환 (0) | 2024.06.17 |
[Android] WorkManager - enqueueUniqueWork() (0) | 2024.06.17 |
[Android] WorkManager - 기초 (0) | 2024.06.17 |