Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is smart to use bindService and when startService

Tags:

android

This may be a stupid question but I would like to know when it is smart to use bindService and when to use startService.

For example:

If I use bindService with BIND_AUTO_CREATE, the service will be started and created automatically as is written here: http://developer.android.com/reference/android/content/Context.html#BIND_AUTO_CREATE

When is it smart then to use bindService and when startService? I really don't understand these two correctly.

like image 676
senzacionale Avatar asked Dec 09 '12 12:12

senzacionale


People also ask

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

Does bindService start service?

The service started with bindService() is bound to the caller (Context), and terminates as soon as the caller closes the service. When the service is started with this method, the service first starts the system and calls the service onCreate() - > onBind ().

What is bindService in Android?

A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).

How can I communicate between two services in Android?

You have to use BroadcastReceiver to receive intents, and when you want to communicate simply make an Intent with appropriate values. This way you should be able to make a 2-way communication between any component.


2 Answers

You usually use bindService() if your calling component(Activity) will need to communicate with the Service that you are starting, through the ServiceConnection. If you do not want to communicate with the Service you can use just startService(). You Can see below diffrence between service and bind service.

From the docs :

Started

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. 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.

Bound

A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

You can read more here : Android Services, Bound Services

like image 112
Ovidiu Latcu Avatar answered Oct 02 '22 13:10

Ovidiu Latcu


I agree with @Ovidiu Latcu but with one important note: when using bound services, the service is ended when the activity that started it is ended, (if it is the only activity bound to that service).

So if you want to run your service at the background while the app is in the background, (the activity is paused for example and not visible to the user) then you must start the service without bounding to it and communicate with it with BroadcastReceiver for example.

like image 31
benchuk Avatar answered Oct 02 '22 13:10

benchuk