Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/android/content/ServiceConnection#pubmethods)

# ServiceConnection

*** ** * ** ***

` public interface ServiceConnection `   
` `

|---|
| android.content.ServiceConnection |

Known indirect subclasses [MediaScannerConnection](https://developer.android.com/reference/android/media/MediaScannerConnection) [MediaScannerConnection](https://developer.android.com/reference/android/media/MediaScannerConnection) MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service.   

*** ** * ** ***

Interface for monitoring the state of an application service. See `https://developer.android.com/reference/android/app/Service` and `https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.Context.BindServiceFlags,%20java.util.concurrent.Executor,%20android.content.ServiceConnection)` for more information.

Like many callbacks from the system, the methods on this class are called from the main thread of your process.

## Summary

| ### Public methods ||
|---|---|
| ` default void` | ` https://developer.android.com/reference/android/content/ServiceConnection#onBindingDied(android.content.ComponentName)(https://developer.android.com/reference/android/content/ComponentName name) ` Called when the binding to this connection is dead. |
| ` default void` | ` https://developer.android.com/reference/android/content/ServiceConnection#onNullBinding(android.content.ComponentName)(https://developer.android.com/reference/android/content/ComponentName name) ` Called when the service being bound has returned `null` from its `https://developer.android.com/reference/android/app/Service#onBind(android.content.Intent)` method. |
| ` abstract void` | ` https://developer.android.com/reference/android/content/ServiceConnection#onServiceConnected(android.content.ComponentName,%20android.os.IBinder)(https://developer.android.com/reference/android/content/ComponentName name, https://developer.android.com/reference/android/os/IBinder service) ` Called when a connection to the Service has been established, with the `https://developer.android.com/reference/android/os/IBinder` of the communication channel to the Service. |
| ` abstract void` | ` https://developer.android.com/reference/android/content/ServiceConnection#onServiceDisconnected(android.content.ComponentName)(https://developer.android.com/reference/android/content/ComponentName name) ` Called when a connection to the Service has been lost. |

## Public methods

### onBindingDied

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onBindingDied (ComponentName name)
```

Called when the binding to this connection is dead. This means the interface will never receive another connection. The application will need to unbind and rebind the connection to activate it again. This may happen, for example, if the application hosting the service it is bound to has been updated.

**Note:** The app that requested the binding must call `https://developer.android.com/reference/android/content/Context#unbindService(android.content.ServiceConnection)` to release the tracking resources associated with this ServiceConnection even if this callback was invoked following `https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.Context.BindServiceFlags,%20java.util.concurrent.Executor,%20android.content.ServiceConnection)`.

<br />

| Parameters ||
|---|---|
| `name` | `ComponentName`: The concrete component name of the service whose connection is dead. <br /> |

### onNullBinding

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onNullBinding (ComponentName name)
```

Called when the service being bound has returned `null` from its `https://developer.android.com/reference/android/app/Service#onBind(android.content.Intent)` method. This indicates that the attempted service binding represented by this ServiceConnection will never become usable.

**Note:** The app that requested the binding must still call `https://developer.android.com/reference/android/content/Context#unbindService(android.content.ServiceConnection)` to release the tracking resources associated with this ServiceConnection even if this callback was invoked following `https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.Context.BindServiceFlags,%20java.util.concurrent.Executor,%20android.content.ServiceConnection)`.

<br />

| Parameters ||
|---|---|
| `name` | `ComponentName`: The concrete component name of the service whose binding has been rejected by the Service implementation. <br /> |

### onServiceConnected

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void onServiceConnected (ComponentName name, 
                IBinder service)
```

Called when a connection to the Service has been established, with the `https://developer.android.com/reference/android/os/IBinder` of the communication channel to the Service.

**Note:** If the system has started to bind your client app to a service, it's possible that your app will never receive this callback. Your app won't receive a callback if there's an issue with the service, such as the service crashing while being created.

<br />

| Parameters ||
|---|---|
| `name` | `ComponentName`: The concrete component name of the service that has been connected. <br /> |
| `service` | `IBinder`: The IBinder of the Service's communication channel, which you can now make calls on. <br /> |

### onServiceDisconnected

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void onServiceDisconnected (ComponentName name)
```

Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does *not* remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to `https://developer.android.com/reference/android/content/ServiceConnection#onServiceConnected(android.content.ComponentName,%20android.os.IBinder)` when the Service is next running.

<br />

| Parameters ||
|---|---|
| `name` | `ComponentName`: The concrete component name of the service whose connection has been lost. <br /> |