Stay organized with collections
Save and categorize content based on your preferences.
ServiceConnection
public
interface
ServiceConnection
android.content.ServiceConnection
|
Known indirect 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
Service
and
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
onBindingDied
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
Context.unbindService(ServiceConnection)
to release the tracking
resources associated with this ServiceConnection even if this callback was
invoked following Context.bindService() bindService()
.
Parameters |
name |
ComponentName : The concrete component name of the service whose connection is dead. |
onNullBinding
public void onNullBinding (ComponentName name)
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 Context.bindService() bindService()
.
Parameters |
name |
ComponentName : The concrete component name of the service whose binding
has been rejected by the Service implementation. |
onServiceConnected
public abstract void onServiceConnected (ComponentName name,
IBinder service)
Called when a connection to the Service has been established, with
the 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
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 onServiceConnected(ComponentName, IBinder)
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](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Methods](#pubmethods) \n\nServiceConnection\n=================\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/android/content/ServiceConnection \"View this page in Kotlin\") \\|Java\n\n\n`\npublic\n\n\ninterface\nServiceConnection\n`\n\n\n`\n\n\n`\n\n|-----------------------------------|\n| android.content.ServiceConnection |\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Known indirect subclasses [MediaScannerConnection](/reference/android/media/MediaScannerConnection) |---------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------| | [MediaScannerConnection](/reference/android/media/MediaScannerConnection) | MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. | |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nInterface for monitoring the state of an application service. See\n[Service](/reference/android/app/Service) and\n[Context.bindService()](/reference/android/content/Context#bindService(android.content.Intent,%20android.content.Context.BindServiceFlags,%20java.util.concurrent.Executor,%20android.content.ServiceConnection)) for more information.\n\nLike many callbacks from the system, the methods on this class are called\nfrom the main thread of your process.\n\nSummary\n-------\n\n| ### Public methods ||\n|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` default void` | ` `[onBindingDied](/reference/android/content/ServiceConnection#onBindingDied(android.content.ComponentName))`(`[ComponentName](/reference/android/content/ComponentName)` name) ` Called when the binding to this connection is dead. |\n| ` default void` | ` `[onNullBinding](/reference/android/content/ServiceConnection#onNullBinding(android.content.ComponentName))`(`[ComponentName](/reference/android/content/ComponentName)` name) ` Called when the service being bound has returned `null` from its [onBind()](/reference/android/app/Service#onBind(android.content.Intent)) method. |\n| ` abstract void` | ` `[onServiceConnected](/reference/android/content/ServiceConnection#onServiceConnected(android.content.ComponentName,%20android.os.IBinder))`(`[ComponentName](/reference/android/content/ComponentName)` name, `[IBinder](/reference/android/os/IBinder)` service) ` Called when a connection to the Service has been established, with the [IBinder](/reference/android/os/IBinder) of the communication channel to the Service. |\n| ` abstract void` | ` `[onServiceDisconnected](/reference/android/content/ServiceConnection#onServiceDisconnected(android.content.ComponentName))`(`[ComponentName](/reference/android/content/ComponentName)` name) ` Called when a connection to the Service has been lost. |\n\nPublic methods\n--------------\n\n### onBindingDied\n\nAdded in [API level 26](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic void onBindingDied (ComponentName name)\n```\n\nCalled when the binding to this connection is dead. This means the\ninterface will never receive another connection. The application will\nneed to unbind and rebind the connection to activate it again. This may\nhappen, for example, if the application hosting the service it is bound to\nhas been updated.\n\n**Note:** The app that requested the binding must call\n[Context.unbindService(ServiceConnection)](/reference/android/content/Context#unbindService(android.content.ServiceConnection)) to release the tracking\nresources associated with this ServiceConnection even if this callback was\ninvoked following [Context.bindService() bindService()](/reference/android/content/Context#bindService(android.content.Intent,%20android.content.Context.BindServiceFlags,%20java.util.concurrent.Executor,%20android.content.ServiceConnection)).\n\n\u003cbr /\u003e\n\n| Parameters ||\n|--------|----------------------------------------------------------------------------------------------|\n| `name` | `ComponentName`: The concrete component name of the service whose connection is dead. \u003cbr /\u003e |\n\n### onNullBinding\n\nAdded in [API level 28](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic void onNullBinding (ComponentName name)\n```\n\nCalled when the service being bound has returned `null` from its\n[onBind()](/reference/android/app/Service#onBind(android.content.Intent)) method. This indicates\nthat the attempted service binding represented by this ServiceConnection\nwill never become usable.\n\n**Note:** The app that requested the binding must still call\n[Context.unbindService(ServiceConnection)](/reference/android/content/Context#unbindService(android.content.ServiceConnection)) to release the tracking\nresources associated with this ServiceConnection even if this callback was\ninvoked following [Context.bindService() bindService()](/reference/android/content/Context#bindService(android.content.Intent,%20android.content.Context.BindServiceFlags,%20java.util.concurrent.Executor,%20android.content.ServiceConnection)).\n\n\u003cbr /\u003e\n\n| Parameters ||\n|--------|-----------------------------------------------------------------------------------------------------------------------------------|\n| `name` | `ComponentName`: The concrete component name of the service whose binding has been rejected by the Service implementation. \u003cbr /\u003e |\n\n### onServiceConnected\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract void onServiceConnected (ComponentName name, \n IBinder service)\n```\n\nCalled when a connection to the Service has been established, with\nthe [IBinder](/reference/android/os/IBinder) of the communication channel to the\nService.\n\n**Note:** If the system has started to bind your\nclient app to a service, it's possible that your app will never receive\nthis callback. Your app won't receive a callback if there's an issue with\nthe service, such as the service crashing while being created.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-----------|--------------------------------------------------------------------------------------------------------|\n| `name` | `ComponentName`: The concrete component name of the service that has been connected. \u003cbr /\u003e |\n| `service` | `IBinder`: The IBinder of the Service's communication channel, which you can now make calls on. \u003cbr /\u003e |\n\n### onServiceDisconnected\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract void onServiceDisconnected (ComponentName name)\n```\n\nCalled when a connection to the Service has been lost. This typically\nhappens when the process hosting the service has crashed or been killed.\nThis does *not* remove the ServiceConnection itself -- this\nbinding to the service will remain active, and you will receive a call\nto [onServiceConnected(ComponentName, IBinder)](/reference/android/content/ServiceConnection#onServiceConnected(android.content.ComponentName,%20android.os.IBinder)) when the Service is next running.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|--------|----------------------------------------------------------------------------------------------------|\n| `name` | `ComponentName`: The concrete component name of the service whose connection has been lost. \u003cbr /\u003e |"]]