Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 208
user1411110 Avatar asked Jun 21 '14 20:06

user1411110


People also ask

What is onBind () meant for?

To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.

What is the difference between startService and bindService?

Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself. A service is "bound" when an application component binds to it by calling bindService().

What is onStartCommand?

onStartCommand() is called every time a client starts the service using startService(Intent intent) . This means that onStartCommand() can get called multiple times. You should do the things in this method that are needed each time a client requests something from your service.

What are the return value of onStartCommand () in Android services?

The documentation says: For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY. So returning super. onStartCommand() is equivalent to returning START_STICKY .


1 Answers

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.

like image 68
Chetan Joshi Avatar answered Oct 14 '22 17:10

Chetan Joshi