JobIntentService는 IntentService와 매우 비슷합니다. 인텐트를 전달하여 서비스에서 어떤 Job을 수행하도록 할 수 있습니다.
차이점은 JobIntentService는 JobScheduler로 동작이 됩니다. JobService는 startService()로 실행했던 반면에 JobIntentService는 enqueueWork()로 실행할 수 있습니다.
Job은 일반적으로 바로 실행되지만 절전모드(Doze mode) 등의 이유로 지연될 수 있습니다. 또한 앱이 Foreground에서 실행중이 아니라면 서비스가 실행되지 않을 수 있습니다. 하지만 JobIntentService를 통해서 예약된 작업은 앱이 background 상태라도 서비스가 실행됩니다.
JobIntentService 구현 및 실행하는 방법에 대해서 알아보겠습니다.
이 글의 예제는 kotlin으로 작성되었습니다.
구현
먼저 JobIntentService
를 상속받는 서비스를 생성합니다.
class MyJobIntentService : JobIntentService() {
companion object {
const val TAG = "MyJobIntentService"
const val JOB_ID = 1001
}
// 1
fun enqueueWork(context: Context, work: Intent) {
enqueueWork(context, MyJobIntentService::class.java, JOB_ID, work)
}
// 2
override fun onCreate() {
super.onCreate()
Log.d(TAG, "Job execution started")
}
// 3
override fun onHandleWork(intent: Intent) {
Log.d(TAG, "MSG: ${intent?.getStringExtra("MSG")}")
for (i in 1..10) {
Thread.sleep(1000)
Log.d(TAG, "onHandleWork() : $i")
}
}
// 4
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "Job execution finished")
}
}
- JobIntentService를 실행할 때 사용하는 method입니다. enqueueWork()는 Job을 추가하는 API입니다.
- JobIntentService가 생성될 때 호출됩니다.
- Job이 추가되면(enqueueWork), onHandleWork()가 callback됩니다.
- 서비스가 종료되면 호출됩니다.
그리고 AndroidManifest.xml
에 다음과 같이 등록해야 합니다.
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".MyJobIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
android.permission.WAKE_LOCK
permission을 등록하는 이유는 Job이 실행될 때 Wake Lock
을 잡을 수 있기 때문입니다.
서비스에 android.permission.BIND_JOB_SERVICE
퍼미션을 등록하는 이유는 JobService만 이 서비스에 접근할 수 있게 만들기 위해서 입니다.
실행
서비스는 다음과 같은 코드로 실행할 수 있습니다.
enqueueWork()
는 Job을 등록하고 scheduling될 수 있게 만듭니다.
val intent = Intent(this, MyJobIntentService::class.java)
intent.putExtra("MSG", "Do something")
startService(intent)
MyJobIntentService().enqueueWork(this,intent)
위 코드 실행 결과는 다음과 같습니다.
12-29 21:36:31.675 9995 10021 D MyJobIntentService: MSG: Do something
12-29 21:36:32.686 9995 10021 D MyJobIntentService: onHandleWork() : 1
12-29 21:36:33.698 9995 10021 D MyJobIntentService: onHandleWork() : 2
12-29 21:36:34.709 9995 10021 D MyJobIntentService: onHandleWork() : 3
12-29 21:36:35.737 9995 10021 D MyJobIntentService: onHandleWork() : 4
12-29 21:36:36.751 9995 10021 D MyJobIntentService: onHandleWork() : 5
12-29 21:36:37.761 9995 10021 D MyJobIntentService: onHandleWork() : 6
12-29 21:36:38.774 9995 10021 D MyJobIntentService: onHandleWork() : 7
12-29 21:36:39.785 9995 10021 D MyJobIntentService: onHandleWork() : 8
12-29 21:36:40.798 9995 10021 D MyJobIntentService: onHandleWork() : 9
12-29 21:36:41.810 9995 10021 D MyJobIntentService: onHandleWork() : 10
12-29 21:36:41.815 9995 9995 D MyJobIntentService: Job execution finished
이 글에서 사용한 예제는 GitHub에서 확인할 수 있습니다.
참고
Recommended Posts:
- Android - 진동, Vibrator, VibrationEffect 예제
- Android - FusedLocationProviderClient으로 위치 정보 얻기
- Android - 시간, 날짜 변경 이벤트 받기
- Android - ACTION_BOOT_COMPLETED 이벤트 받기
- Android - Foreground Service 실행
- Android - GPS, Network 위치 정보 얻기 (LocationManager)
- Android - Screen On/Off 이벤트 수신, 상태 확인
- Android - 파일 입출력 예제 (Read, Write, 내부, 외부 저장소)
- Android - currentTimeMillis(), elapsedRealtime(), uptimeMillis()
- Android - PowerManager WakeLock
- Android - 다른 앱의 Service에 바인딩
- Android - Handler vs Executor
- Android - Darkmode 활성화하는 방법