/* * 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 android.arch.lifecycle; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.CallSuper; import android.support.annotation.Nullable; /** * A Service that is also a {@link LifecycleOwner}. */ public class LifecycleService extends Service implements LifecycleOwner { private final ServiceLifecycleDispatcher mDispatcher = new ServiceLifecycleDispatcher(this); @CallSuper @Override public void onCreate() { mDispatcher.onServicePreSuperOnCreate(); super.onCreate(); } @CallSuper @Nullable @Override public IBinder onBind(Intent intent) { mDispatcher.onServicePreSuperOnBind(); return null; } @SuppressWarnings("deprecation") @CallSuper @Override public void onStart(Intent intent, int startId) { mDispatcher.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 mDispatcher.onServicePreSuperOnStart() call, because // super.onStartCommand calls onStart(). @CallSuper @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @CallSuper @Override public void onDestroy() { mDispatcher.onServicePreSuperOnDestroy(); super.onDestroy(); } @Override public Lifecycle getLifecycle() { return mDispatcher.getLifecycle(); } }