1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package androidx.lifecycle 17 18 import android.app.Service 19 import android.content.Intent 20 import android.os.IBinder 21 import androidx.annotation.CallSuper 22 23 /** A Service that is also a [LifecycleOwner]. */ 24 public open class LifecycleService : Service(), LifecycleOwner { 25 26 private val dispatcher = ServiceLifecycleDispatcher(this) 27 28 @CallSuper onCreatenull29 override fun onCreate() { 30 dispatcher.onServicePreSuperOnCreate() 31 super.onCreate() 32 } 33 34 @CallSuper onBindnull35 override fun onBind(intent: Intent): IBinder? { 36 dispatcher.onServicePreSuperOnBind() 37 return null 38 } 39 40 @Deprecated("Deprecated in Java") 41 @Suppress("DEPRECATION") 42 @CallSuper onStartnull43 override fun onStart(intent: Intent?, startId: Int) { 44 dispatcher.onServicePreSuperOnStart() 45 super.onStart(intent, startId) 46 } 47 48 // this method is added only to annotate it with @CallSuper. 49 // In usual Service, super.onStartCommand is no-op, but in LifecycleService 50 // it results in dispatcher.onServicePreSuperOnStart() call, because 51 // super.onStartCommand calls onStart(). 52 @CallSuper onStartCommandnull53 override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 54 return super.onStartCommand(intent, flags, startId) 55 } 56 57 @CallSuper onDestroynull58 override fun onDestroy() { 59 dispatcher.onServicePreSuperOnDestroy() 60 super.onDestroy() 61 } 62 63 override val lifecycle: Lifecycle 64 get() = dispatcher.lifecycle 65 } 66