How it is possible Service run indefinitely and also allow binding in android?

Service.onStartCommand callback will be called only when you start your service using startService method. As @NickT and @JoelF already pointed you need to call startService() besides the bindService() call somewhere in your client code (e.g. in onCreate).

You might also want to have a look at this (a little bit old, but still useful) article: "Double life of a service" and try the example program author provided.


I've not used services with the messenger service, but I have bound to a remote service with a remote (AIDL) interface. My findings may be of some help. As my main activity and service are currently implemented, I bind to the service like you do with code like

mServiceConnected = bindService(new Intent("com.mypackage.MyService.SERVICE"), this,
                Context.BIND_AUTO_CREATE);

My activity implements ServiceConnection

When I call unbindService(this) as the activity ends, then like you have found, the service's onDestroy() method is called.

If however, prior to the bindService line, I also explicitly start the service with

startService(new Intent("com.mypackage.MyService.SERVICE"));

then the unBind does not cause the service's onDestroy() to execute. It's still necessary call unbindService in the activity's onDestroy/Stop, otherwise you will leak a service connection.

In my case, presumably the service remains available for other applications to bind to via its remote interface.


You just need to start it with startService() somewhere. This will prevent it from being stopped automatically when there are no more bindings.

From the Service documentation, emphasis mine:

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.

As others have pointed out, it could still be killed by Android if resources are needed. You can "prioritize" your Service and make it less likely to be killed if you make it a foreground service.


In order to perform communication between service and activity. You can also use Binder as mentioned in Official Android Example

http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

As official android documents suggests http://developer.android.com/guide/components/services.html#StartingAService

Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding. It's simply a matter of whether you implement a couple callback methods: onStartCommand() to allow components to start it and onBind() to allow binding.

This project implement this mixture of service (BothService) and shows that how can a Service run indefinitely and also allow binding with multiple activities.

https://github.com/shanrais/BothService