public static void onStop ( Service service , Runnable terminatedCallback , Consumer < Throwable > failureCallback , Executor executor ) { ShutdownListener listener = new ShutdownListener ( terminatedCallback , failureCallback ) ; service . addListener ( listener , executor ) ; // addListener() will not invoke the callbacks if the service is already in a terminal state. As such, we need to // manually check for these states after registering the listener and invoke the appropriate callback. The // ShutdownListener will make sure they are not invoked multiple times. Service . State state = service . state ( ) ; if ( state == Service . State . FAILED ) { // We don't care (or know) the state from which we came, so we just pass some random one. listener . failed ( Service . State . FAILED , service . failureCause ( ) ) ; } else if ( state == Service . State . TERMINATED ) { listener . terminated ( Service . State . TERMINATED ) ; } }
Attaches the given callbacks which will be invoked when the given Service enters a TERMINATED or FAILED state . The callbacks are optional and may be invoked synchronously if the Service is already in one of these states .