public class

LifecycleService

extends Service

implements LifecycleOwner

 java.lang.Object

↳Service

↳androidx.lifecycle.LifecycleService

Subclasses:

SystemForegroundService, SystemAlarmService

Gradle dependencies

compile group: 'androidx.lifecycle', name: 'lifecycle-service', version: '2.5.0-rc01'

  • groupId: androidx.lifecycle
  • artifactId: lifecycle-service
  • version: 2.5.0-rc01

Artifact androidx.lifecycle:lifecycle-service:2.5.0-rc01 it located at Google repository (https://maven.google.com/)

Androidx class mapping:

androidx.lifecycle.LifecycleService android.arch.lifecycle.LifecycleService

Overview

A Service that is also a LifecycleOwner.

Summary

Constructors
publicLifecycleService()

Methods
public LifecyclegetLifecycle()

public IBinderonBind(Intent intent)

public voidonCreate()

public voidonDestroy()

public voidonStart(Intent intent, int startId)

public intonStartCommand(Intent intent, int flags, int startId)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public LifecycleService()

Methods

public void onCreate()

public IBinder onBind(Intent intent)

public void onStart(Intent intent, int startId)

public int onStartCommand(Intent intent, int flags, int startId)

public void onDestroy()

public Lifecycle getLifecycle()

Source

/*
 * 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;
import androidx.annotation.NonNull;
import androidx.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(@NonNull Intent intent) {
        mDispatcher.onServicePreSuperOnBind();
        return null;
    }

    @SuppressWarnings("deprecation")
    @CallSuper
    @Override
    public void onStart(@Nullable 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(@Nullable Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @CallSuper
    @Override
    public void onDestroy() {
        mDispatcher.onServicePreSuperOnDestroy();
        super.onDestroy();
    }

    @Override
    @NonNull
    public Lifecycle getLifecycle() {
        return mDispatcher.getLifecycle();
    }
}