# ServiceLifecycleObserver

Observer for Service lifecycle events.

Use this interface to track lifecycle changes of SDK Services.

## ServiceLifecycleObserver API

### `onServiceCreated`

> ```java
> void onServiceCreated(Class<? extends Service> cls)
> ```
>
> Called when a Service used by the SDK is created. `cls` is the Service class that was created.

### `onServiceDestroyed`

> ```java
> void onServiceDestroyed(Class<? extends Service> cls)
> ```
>
> Called when a Service used by the SDK is destroyed. `cls` is the Service class that was destroyed.

### `onForegroundStarted`&#x20;

> <pre class="language-java"><code class="lang-java"><strong>void onForegroundStarted(Class&#x3C;? extends Service> cls)
> </strong></code></pre>
>
> Called when a Service used by the SDK starts running in the foreground. `cls` is the Service class that started foreground execution.

### `onForegroundStopped`

> ```java
> void onForegroundStopped(Class<? extends Service> cls)
> ```
>
> Called when a Service used by the SDK stops running in the foreground. `cls` is the Service class that stopped foreground execution.

## Threading

> Observer callbacks are posted on the main thread.

## Example

```java
ServiceLifecycleObserver observer = new ServiceLifecycleObserver() {
    @Override
    public void onServiceCreated(Class<? extends Service> cls) {
        // Service created.
    }

    @Override
    public void onServiceDestroyed(Class<? extends Service> cls) {
        // Service destroyed.
    }

    @Override
    public void onForegroundStarted(Class<? extends Service> cls) {
        // Foreground started.
    }

    @Override
    public void onForegroundStopped(Class<? extends Service> cls) {
        // Foreground stopped.
    }
};

SdkDiagnostics diagnostics =
        new SdkDiagnostics.Builder()
                .serviceLifecycleObserver(observer)
                .build();

SentianceOptions options =
        new SentianceOptions.Builder(context)
                .diagnostics(diagnostics)
                .build();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sentiance.com/important-topics/sdk/api-reference/android/sdkdiagnostics/servicelifecycleobserver.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
