Partager via


Context.BindService Method

Definition

Overloads

BindService(Intent, IServiceConnection, Bind)

Connects to an application service, creating it if needed.

BindService(Intent, IServiceConnection, Context+BindServiceFlags)
BindService(Intent, Bind, IExecutor, IServiceConnection)

Same as #bindService(Intent, ServiceConnection, int) bindService(Intent, ServiceConnection, int) with executor to control ServiceConnection callbacks.

BindService(Intent, Context+BindServiceFlags, IExecutor, IServiceConnection)

BindService(Intent, IServiceConnection, Bind)

Connects to an application service, creating it if needed.

[Android.Runtime.Register("bindService", "(Landroid/content/Intent;Landroid/content/ServiceConnection;I)Z", "GetBindService_Landroid_content_Intent_Landroid_content_ServiceConnection_IHandler")]
public abstract bool BindService (Android.Content.Intent service, Android.Content.IServiceConnection conn, Android.Content.Bind flags);
[<Android.Runtime.Register("bindService", "(Landroid/content/Intent;Landroid/content/ServiceConnection;I)Z", "GetBindService_Landroid_content_Intent_Landroid_content_ServiceConnection_IHandler")>]
abstract member BindService : Android.Content.Intent * Android.Content.IServiceConnection * Android.Content.Bind -> bool

Parameters

service
Intent

Identifies the service to connect to. The Intent must specify an explicit component name.

conn
IServiceConnection

Receives information as the service is started and stopped. This must be a valid ServiceConnection object; it must not be null.

flags
Bind

Operation options for the binding. Can be: <ul> <li>0 <li>#BIND_AUTO_CREATE<li>#BIND_DEBUG_UNBIND<li>#BIND_NOT_FOREGROUND<li>#BIND_ABOVE_CLIENT<li>#BIND_ALLOW_OOM_MANAGEMENT<li>#BIND_WAIVE_PRIORITY<li>#BIND_IMPORTANT<li>#BIND_ADJUST_WITH_ACTIVITY<li>#BIND_NOT_PERCEPTIBLE<li>#BIND_INCLUDE_CAPABILITIES</ul>

Returns

true if the system is in the process of bringing up a service that your client has permission to bind to; false if the system couldn't find the service or if your client doesn't have permission to bind to it. Regardless of the return value, you should later call #unbindService to release the connection.

Attributes

Exceptions

Remarks

Connects to an application service, creating it if needed. This defines a dependency between your application and the service. The given <var>conn</var> will receive the service object when it is created and be told if it dies and restarts. The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.

If the service does not support binding, it may return null from its android.app.Service#onBind(Intent) onBind() method. If it does, then the ServiceConnection's ServiceConnection#onNullBinding(ComponentName) onNullBinding() method will be invoked instead of ServiceConnection#onServiceConnected(ComponentName, IBinder) onServiceConnected().

<p class="note"><b>Note:</b> This method <em>cannot</em> be called from a BroadcastReceiver component. A pattern you can use to communicate from a BroadcastReceiver to a Service is to call #startService with the arguments containing the command to be sent, with the service calling its android.app.Service#stopSelf(int) method when done executing that command. See the API demo App/Service/Service Start Arguments Controller for an illustration of this. It is okay, however, to use this method from a BroadcastReceiver that has been registered with #registerReceiver, since the lifetime of this BroadcastReceiver is tied to another object (the one that registered it).

This method only accepts a int type flag, to pass in a long type flag, call #bindService(Intent, ServiceConnection, BindServiceFlags) instead.

Java documentation for android.content.Context.bindService(android.content.Intent, android.content.ServiceConnection, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to

BindService(Intent, IServiceConnection, Context+BindServiceFlags)

[Android.Runtime.Register("bindService", "(Landroid/content/Intent;Landroid/content/ServiceConnection;Landroid/content/Context$BindServiceFlags;)Z", "GetBindService_Landroid_content_Intent_Landroid_content_ServiceConnection_Landroid_content_Context_BindServiceFlags_Handler", ApiSince=34)]
public virtual bool BindService (Android.Content.Intent service, Android.Content.IServiceConnection conn, Android.Content.Context.BindServiceFlags flags);
[<Android.Runtime.Register("bindService", "(Landroid/content/Intent;Landroid/content/ServiceConnection;Landroid/content/Context$BindServiceFlags;)Z", "GetBindService_Landroid_content_Intent_Landroid_content_ServiceConnection_Landroid_content_Context_BindServiceFlags_Handler", ApiSince=34)>]
abstract member BindService : Android.Content.Intent * Android.Content.IServiceConnection * Android.Content.Context.BindServiceFlags -> bool
override this.BindService : Android.Content.Intent * Android.Content.IServiceConnection * Android.Content.Context.BindServiceFlags -> bool

Parameters

service
Intent

Returns

Attributes

Applies to

BindService(Intent, Bind, IExecutor, IServiceConnection)

Same as #bindService(Intent, ServiceConnection, int) bindService(Intent, ServiceConnection, int) with executor to control ServiceConnection callbacks.

[Android.Runtime.Register("bindService", "(Landroid/content/Intent;ILjava/util/concurrent/Executor;Landroid/content/ServiceConnection;)Z", "GetBindService_Landroid_content_Intent_ILjava_util_concurrent_Executor_Landroid_content_ServiceConnection_Handler", ApiSince=29)]
public virtual bool BindService (Android.Content.Intent service, Android.Content.Bind flags, Java.Util.Concurrent.IExecutor executor, Android.Content.IServiceConnection conn);
[<Android.Runtime.Register("bindService", "(Landroid/content/Intent;ILjava/util/concurrent/Executor;Landroid/content/ServiceConnection;)Z", "GetBindService_Landroid_content_Intent_ILjava_util_concurrent_Executor_Landroid_content_ServiceConnection_Handler", ApiSince=29)>]
abstract member BindService : Android.Content.Intent * Android.Content.Bind * Java.Util.Concurrent.IExecutor * Android.Content.IServiceConnection -> bool
override this.BindService : Android.Content.Intent * Android.Content.Bind * Java.Util.Concurrent.IExecutor * Android.Content.IServiceConnection -> bool

Parameters

service
Intent
flags
Bind
executor
IExecutor

Callbacks on ServiceConnection will be called on executor. Must use same instance for the same instance of ServiceConnection.

Returns

The result of the binding as described in #bindService(Intent, ServiceConnection, int) bindService(Intent, ServiceConnection, int).

Attributes

Remarks

Same as #bindService(Intent, ServiceConnection, int) bindService(Intent, ServiceConnection, int) with executor to control ServiceConnection callbacks.

This method only accepts a 32 bits flag, to pass in a 64 bits flag, call #bindService(Intent, BindServiceFlags, Executor, ServiceConnection) instead.

Java documentation for android.content.Context.bindService(android.content.Intent, int, java.util.concurrent.Executor, android.content.ServiceConnection).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

BindService(Intent, Context+BindServiceFlags, IExecutor, IServiceConnection)

[Android.Runtime.Register("bindService", "(Landroid/content/Intent;Landroid/content/Context$BindServiceFlags;Ljava/util/concurrent/Executor;Landroid/content/ServiceConnection;)Z", "GetBindService_Landroid_content_Intent_Landroid_content_Context_BindServiceFlags_Ljava_util_concurrent_Executor_Landroid_content_ServiceConnection_Handler", ApiSince=34)]
public virtual bool BindService (Android.Content.Intent service, Android.Content.Context.BindServiceFlags flags, Java.Util.Concurrent.IExecutor executor, Android.Content.IServiceConnection conn);
[<Android.Runtime.Register("bindService", "(Landroid/content/Intent;Landroid/content/Context$BindServiceFlags;Ljava/util/concurrent/Executor;Landroid/content/ServiceConnection;)Z", "GetBindService_Landroid_content_Intent_Landroid_content_Context_BindServiceFlags_Ljava_util_concurrent_Executor_Landroid_content_ServiceConnection_Handler", ApiSince=34)>]
abstract member BindService : Android.Content.Intent * Android.Content.Context.BindServiceFlags * Java.Util.Concurrent.IExecutor * Android.Content.IServiceConnection -> bool
override this.BindService : Android.Content.Intent * Android.Content.Context.BindServiceFlags * Java.Util.Concurrent.IExecutor * Android.Content.IServiceConnection -> bool

Parameters

service
Intent
executor
IExecutor

Returns

Attributes

Applies to