Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android service startService() and bindService()

Tags:

android

I would like to know if it is possible to have a service that is started with startService and then be able to also bind to that service and do some remote procedure calls? according to this:http://developer.android.com/guide/topics/fundamentals.html#servlife

the two services have different lifecycle so it's not possible,does anyone know about it?

like image 386
maxsap Avatar asked Aug 18 '10 16:08

maxsap


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

When startService () is called which service get created?

A started service is one that another component starts by calling startService() , which results in a call to the service's onStartCommand() method. When a service is started, it has a lifecycle that's independent of the component that started it.

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.


1 Answers

I think hara's answer was a little confusing. What you describe is perfectly legitimate and in fact the only way to get the behavior you want. If you create a Service by binding to it, it will die when you unbind. So the only way to keep it around without activities binding to it is to start it with startService(). There is no conflict with lifecycles because it only applies to how the service is STARTED. So once it's started with startService(), it follows that lifecycle process. So you are free to bind and unbind to it as much as you wish and it will only die when you call stopService() or stopSelf().

like image 180
Falmarri Avatar answered Oct 06 '22 00:10

Falmarri