Cola
Cola

Reputation: 2257

Difference between onStartCommand() and onBind()

How is the "bind" action of the onBind() method different than just calling onStartCommand() ?

onStartCommand()

"The system calls this method when another component, such as an activity, requests that the service be started, by calling startService()."

onBind()

The system calls this method when another component wants to bind with the service (such as to perform RPC), by calling bindService().

I want to write a chat client service which receives messages from multiple users. Which function would be more appropriate?

Upvotes: 13

Views: 10207

Answers (5)

Umair Mustafa
Umair Mustafa

Reputation: 218

According to the official documentation, when a service is started using bindService()

if a component(i.e Activity) calls bindService() to create the service and onStartCommand() is not called, the service runs only as long as the component is bound to it. After the service is unbound from all of its clients, the system destroys it.

and when a service is started using startService()

If a component starts the service by calling startService() (which results in a call to onStartCommand()), the service continues to run until it stops itself with stopSelf() or another component stops it by calling stopService().

Upvotes: 0

Sanam Yavarpor
Sanam Yavarpor

Reputation: 354

In fact, if a component calls bindService() to create the service and onStartCommand() is not called, the service runs only as long as the component is bound to it. After the service is unbound from all of its clients, the system destroys it. So, by onBind() if the activity destroy your service would not trigger.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93614

A bound service will end when it has no more activities bound to it. Binding also allows you to send additional commands to it via interfaces like AIDL. In your case, I think you'd want a bound service, as you likely don't want the service to outlive the activity.

Upvotes: 2

Chetan Joshi
Chetan Joshi

Reputation: 5711

onStartCommand() and onBind() are callback methods of Service class.

onStartCommand() called after onCreate() method of Service class first time.Next time whenever any other android component start same service then Service received new request in onStartCommand() method.

onBind() called when another Android components try to connect with already running Service by using bindService() method .Its used to pass some new info to service or try to make Service connection.

Upvotes: 4

indivisible
indivisible

Reputation: 5012

The first (onStartCommand()) is called when your Service begins to do its work. onCreate() has completed and it is ready to get to doing what needs to be done.

The second (onBind()) is called when another Thread registers to connect to the Service so that they can communicate. You would configure or set up the means for the communication in here such as Interface validation or calls back to the registering Activity.

Binding allows you to tie the Service to the lifespan of, for example, an Activity. If the Activity completes then the Service is allowed to be released and can itself finish. The Service will last as long as there is something still bound to it.

Upvotes: 12

Related Questions