Stay organized with collections
Save and categorize content based on your preferences.
ServiceConnection
interface ServiceConnection
Known Direct Subclasses
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 android.app.Service
and android.content.Context#bindService 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 |
open Unit |
Called when the binding to this connection is dead.
|
open Unit |
Called when the service being bound has returned null from its onBind() method.
|
abstract Unit |
Called when a connection to the Service has been established, with the android.os.IBinder of the communication channel to the Service.
|
abstract Unit |
Called when a connection to the Service has been lost.
|
Public methods
onBindingDied
open fun onBindingDied(name: ComponentName!): Unit
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 Context.unbindService(ServiceConnection)
to release the tracking resources associated with this ServiceConnection even if this callback was invoked following android.content.Context#bindService.
Parameters |
name |
ComponentName!: The concrete component name of the service whose connection is dead. |
onNullBinding
open fun onNullBinding(name: ComponentName!): Unit
Called when the service being bound has returned null
from its onBind()
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 Context.unbindService(ServiceConnection)
to release the tracking resources associated with this ServiceConnection even if this callback was invoked following android.content.Context#bindService.
Parameters |
name |
ComponentName!: The concrete component name of the service whose binding has been rejected by the Service implementation. |
onServiceConnected
abstract fun onServiceConnected(
name: ComponentName!,
service: IBinder!
): Unit
Called when a connection to the Service has been established, with the 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.
Parameters |
name |
ComponentName!: The concrete component name of the service that has been connected. |
service |
IBinder!: The IBinder of the Service's communication channel, which you can now make calls on. |
onServiceDisconnected
abstract fun onServiceDisconnected(name: ComponentName!): Unit
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 onServiceConnected
when the Service is next running.
Parameters |
name |
ComponentName!: The concrete component name of the service whose connection has been lost. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# ServiceConnection\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nServiceConnection\n=================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/content/ServiceConnection \"View this page in Java\") \n\n```\ninterface ServiceConnection\n```\n\n|----------------------------------------|\n| [android.content.ServiceConnection](#) |\n\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Known Direct Subclasses [MediaScannerConnection](../media/MediaScannerConnection.html#) |-----------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------| | [MediaScannerConnection](../media/MediaScannerConnection.html#) | MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. | |\n\nInterface for monitoring the state of an application service. See [android.app.Service](../app/Service.html#) and android.content.Context#bindService for more information.\n\nLike many callbacks from the system, the methods on this class are called from the main thread of your process.\n\nSummary\n-------\n\n| Public methods ||\n|---------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onBindingDied](#onBindingDied(android.content.ComponentName))`(`name:` `[ComponentName](/reference/kotlin/android/content/ComponentName)!`)` Called when the binding to this connection is dead. |\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onNullBinding](#onNullBinding(android.content.ComponentName))`(`name:` `[ComponentName](/reference/kotlin/android/content/ComponentName)!`)` Called when the service being bound has returned `null` from its [onBind()](../app/Service.html#onBind(android.content.Intent)) method. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onServiceConnected](#onServiceConnected(android.content.ComponentName,%20android.os.IBinder))`(`name:` `[ComponentName](/reference/kotlin/android/content/ComponentName)!`, `service:` `[IBinder](../os/IBinder.html#)!`)` Called when a connection to the Service has been established, with the [android.os.IBinder](../os/IBinder.html#) of the communication channel to the Service. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onServiceDisconnected](#onServiceDisconnected(android.content.ComponentName))`(`name:` `[ComponentName](/reference/kotlin/android/content/ComponentName)!`)` Called when a connection to the Service has been lost. |\n\nPublic methods\n--------------\n\n### onBindingDied\n\nAdded in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun onBindingDied(name: ComponentName!): Unit\n```\n\nCalled 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.\n\n**Note:** The app that requested the binding must call [Context.unbindService(ServiceConnection)](/reference/kotlin/android/content/Context#unbindService(android.content.ServiceConnection)) to release the tracking resources associated with this ServiceConnection even if this callback was invoked following android.content.Context#bindService.\n\n| Parameters ||\n|--------|-----------------------------------------------------------------------------------------------------------------------------------------|\n| `name` | [ComponentName](/reference/kotlin/android/content/ComponentName)!: The concrete component name of the service whose connection is dead. |\n\n### onNullBinding\n\nAdded in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun onNullBinding(name: ComponentName!): Unit\n```\n\nCalled when the service being bound has returned `null` from its [onBind()](../app/Service.html#onBind(android.content.Intent)) method. This indicates that the attempted service binding represented by this ServiceConnection will never become usable.\n\n**Note:** The app that requested the binding must still call [Context.unbindService(ServiceConnection)](/reference/kotlin/android/content/Context#unbindService(android.content.ServiceConnection)) to release the tracking resources associated with this ServiceConnection even if this callback was invoked following android.content.Context#bindService.\n\n| Parameters ||\n|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `name` | [ComponentName](/reference/kotlin/android/content/ComponentName)!: The concrete component name of the service whose binding has been rejected by the Service implementation. |\n\n### onServiceConnected\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onServiceConnected(\n name: ComponentName!, \n service: IBinder!\n): Unit\n```\n\nCalled when a connection to the Service has been established, with the [android.os.IBinder](../os/IBinder.html#) of the communication channel to the Service.\n\n**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.\n\n| Parameters ||\n|-----------|----------------------------------------------------------------------------------------------------------------------------------------|\n| `name` | [ComponentName](/reference/kotlin/android/content/ComponentName)!: The concrete component name of the service that has been connected. |\n| `service` | [IBinder](../os/IBinder.html#)!: The IBinder of the Service's communication channel, which you can now make calls on. |\n\n### onServiceDisconnected\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onServiceDisconnected(name: ComponentName!): Unit\n```\n\nCalled 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 [onServiceConnected](#onServiceConnected(android.content.ComponentName,%20android.os.IBinder)) when the Service is next running.\n\n| Parameters ||\n|--------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| `name` | [ComponentName](/reference/kotlin/android/content/ComponentName)!: The concrete component name of the service whose connection has been lost. |"]]