1. LifecycleService is introduced

In the previous article we described how LifeCycle can be used to listen for the LifeCycle of a page for decoupling purposes; As we know, in addition to activities and fragments, there is also a very important component of Android system with the concept of life cycle: Service. Jetpack provides the LifecycleService component for Service lifecycle decoupling. LifecycleService inherits Service and implements the LifecycleOwner interface. LifecycleService source code:

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); } @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(); }}Copy the code

2. Use LifecycleService to listen to the Service life cycle

(1) First we need to add dependencies to the build.gradle file of our app:

Dependencies {implementation 'androidx. Lifecycle: lifecycle - extensions: 2.2.0'}Copy the code

(2) create a sense of Service to monitor LifecycleObserver PerceptionServiceListener implementation, using the @ OnLifecycleEvent annotations, defined on the life cycle of a required; The specific code is as follows:

public class PerceptionServiceListener implements LifecycleObserver { private static final String TAG = "PerceptionServiceListener"; @SuppressLint("LongLogTag") @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) private void onCreate() { Log.d(TAG, "onCreate"); } @SuppressLint("LongLogTag") @OnLifecycleEvent(Lifecycle.Event.ON_START) private void onStart() { Log.d(TAG, "onStart"); } @SuppressLint("LongLogTag") @OnLifecycleEvent(Lifecycle.Event.ON_STOP) private void onStop() { Log.d(TAG, "onStop"); } @SuppressLint("LongLogTag") @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) private void onDestroy() { Log.d(TAG, "onDestroy"); }}Copy the code

(3) to create custom Service LifecycleService inheritance and register PerceptionServiceListener, source code is as follows:

public class CustomService extends LifecycleService { public CustomService(){ PerceptionServiceListener perceptionServiceListener = new PerceptionServiceListener(); getLifecycle().addObserver(perceptionServiceListener); }}Copy the code

(4) Start the Service and look at the result:

At this point, listening for the Service life cycle using LifecycleService is complete, and in the next article we’ll look at ProcessLifecycleOwner listening for the application life cycle.