Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want a service which can run in the background until I stop, even if the component that started it is destroyed and also allows binding to the activities. How it is possible ?

As per android bound services document - there are three ways of creating bound service

  1. Extending Binder class.
  2. Using Messenger.
  3. Using AIDL.

I have created a bound service using messenger (2nd method). Activity is bind to service in its onStart() method and unbind in its onStop() method. Two way messaging (between activity and service) works properly. But problem is that when activity unbinds service, service is destroyed. But I want a service which can run indefinitely.

It is possible as android Services Dev Guide - "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."

I also implement onStartCommand() method in service and return START_STICKY, but it is never called. Looking at the lifecycle callbacks of bounded service in dev guide, there is no onStartCommand() callback method. Then how it is possible to run service until we stop and also allow binding?

I am using eclipse platform in fedora 15 OS.

Any Help.....

like image 344
Khushbu Shah Avatar asked Sep 03 '11 10:09

Khushbu Shah


People also ask

What is service binding 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).

What is the difference between bound and unbound service in Android?

The Unbound service runs in the background indefinitely. Where As The Bound service does not run in the background indefinitely. The Unbound service is stopped by stopService() method. Where As In The Bound service, The client can unbind the service by calling the unbindService() method.

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.


2 Answers

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.

like image 96
Joel F Avatar answered Oct 04 '22 20:10

Joel F


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.

like image 35
NickT Avatar answered Oct 04 '22 18:10

NickT