Context.BindService Method

Definition

Overloads

Name Description
BindService(Intent, IServiceConnection, Bind)

Connects to an application service, creating it if needed.

BindService(Intent, IServiceConnection, Context+BindServiceFlags)

See bindService(Intent,ServiceConnection,int) Call BindServiceFlags.of(long) to obtain a BindServiceFlags object.

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)

See bindService(Intent,int,Executor,ServiceConnection) Call BindServiceFlags.of(long) to obtain a BindServiceFlags object.

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().

<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)

See bindService(Intent,ServiceConnection,int) Call BindServiceFlags.of(long) to obtain a BindServiceFlags object.

[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

This value cannot be null .

conn
IServiceConnection

This value cannot be null .

flags
Context.BindServiceFlags

This value cannot be null .

Returns

See bindService(Intent,ServiceConnection,int) Call BindServiceFlags.of(long) to obtain a BindServiceFlags object.

Attributes

Remarks

See bindService(Intent,ServiceConnection,int) Call BindServiceFlags.of(long) to obtain a BindServiceFlags object.

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

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, 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

This value cannot be null .

flags
Bind

Value is either 0 or a combination of the following: BIND_AUTO_CREATE BIND_DEBUG_UNBIND BIND_NOT_FOREGROUND BIND_ABOVE_CLIENT BIND_ALLOW_OOM_MANAGEMENT BIND_WAIVE_PRIORITY BIND_IMPORTANT BIND_ADJUST_WITH_ACTIVITY BIND_NOT_PERCEPTIBLE BIND_ALLOW_ACTIVITY_STARTS BIND_INCLUDE_CAPABILITIES BIND_SHARED_ISOLATED_PROCESS BIND_PACKAGE_ISOLATED_PROCESS BIND_EXTERNAL_SERVICE

executor
IExecutor

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

conn
IServiceConnection

This value cannot be null .

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)

See bindService(Intent,int,Executor,ServiceConnection) Call BindServiceFlags.of(long) to obtain a BindServiceFlags object.

[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

This value cannot be null .

flags
Context.BindServiceFlags

This value cannot be null .

executor
IExecutor

This value cannot be null . Callback and listener events are dispatched through this Executor , providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor() . Otherwise, provide an Executor that dispatches to an appropriate thread.

conn
IServiceConnection

This value cannot be null .

Returns

See bindService(Intent,int,Executor,ServiceConnection) Call BindServiceFlags.of(long) to obtain a BindServiceFlags object.

Attributes

Remarks

See bindService(Intent,int,Executor,ServiceConnection) Call BindServiceFlags.of(long) to obtain a BindServiceFlags object.

Java documentation for android.content.Context.bindService(android.content.Intent, android.content.Context.BindServiceFlags, 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