Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android bindService or/and startService

Tags:

android

I want to create Service using bindService method. But when I close one Activity my Service is destroyed, and I don't want that.

I try to put service in foreground using startForeground(NOTIFICATION_ID, notification); service onCreate , but service still destroy.

Now I try with call two methods for starting Service at same time :

    Intent bindIntent= new Intent(this, ServiceC.class);
    startService(bindIntent);
    bindService(bindIntent, onService, BIND_AUTO_CREATE);

By calling these two methods Service not destroyed. My app work fine with this method.

Can someone explain to me whether this is a good way or if it is not can you please give me idea why startForeground(NOTIFICATION_ID, notification); does not work ?

What is the best way to use bindService but at the same time I don't want the service to self destroy.

like image 318
Jovan Avatar asked Sep 11 '10 01:09

Jovan


People also ask

What is difference between started and bound services in Android?

Started services run until they are stopped or destroyed and do not inherently provide a mechanism for interaction or data exchange with other components. Bound services, on the other hand, provide a communication interface to other client components and generally run until the last client unbinds from the service.

Does bindService start service?

Binding to a started service As discussed in the Services document, you can create a service that is both started and bound. That is, you can start a service by calling startService() , which allows the service to run indefinitely, and you can also allow a client to bind to the service by calling bindService() .

What is bound and unbound service in Android?

Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive. while a unbounded service will work till the completion even after activity is destroyed.

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 Used the same solution and it's a legitimate one. From Service ref:

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated.

startForeground() is not working because it just tries to prevent the service from being killed by the system, but its lifecycle is another thing: if nothing is more bound to that service and it wasn't started, it just stops.

like image 93
bigstones Avatar answered Oct 14 '22 20:10

bigstones