LifecycleService.kt
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.lifecycle
import android.app.Service
import android.content.Intent
import android.os.IBinder
import androidx.annotation.CallSuper
/**
* A Service that is also a [LifecycleOwner].
*/
open class LifecycleService : Service(), LifecycleOwner {
private val dispatcher = ServiceLifecycleDispatcher(this)
@CallSuper
override fun onCreate() {
dispatcher.onServicePreSuperOnCreate()
super.onCreate()
}
@CallSuper
override fun onBind(intent: Intent): IBinder? {
dispatcher.onServicePreSuperOnBind()
return null
}
@Deprecated("Deprecated in Java")
@Suppress("DEPRECATION")
@CallSuper
override fun onStart(intent: Intent?, startId: Int) {
dispatcher.onServicePreSuperOnStart()
super.onStart(intent, startId)
}
// this method is added only to annotate it with @CallSuper.
// In usual Service, super.onStartCommand is no-op, but in LifecycleService
// it results in dispatcher.onServicePreSuperOnStart() call, because
// super.onStartCommand calls onStart().
@CallSuper
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
}
@CallSuper
override fun onDestroy() {
dispatcher.onServicePreSuperOnDestroy()
super.onDestroy()
}
override val lifecycle: Lifecycle
get() = dispatcher.lifecycle
}