bindService() as Option?

150 views
Skip to first unread message

jtaylor

unread,
Jan 31, 2008, 7:52:22 PM1/31/08
to Android Developers
If when no activities are bound to a service, why not have the option
for the service to continue?

hackbod

unread,
Jan 31, 2008, 9:27:07 PM1/31/08
to Android Developers
That's what startService() lets you do.
Message has been deleted

jtaylor

unread,
Jan 31, 2008, 10:26:08 PM1/31/08
to Android Developers
In using bindService, if there are no activities bound to the service,
then the service stops running? If so, suppose I don't want that to
happen. Suppose I want the service to keep running even when all
activities that are bound to it are unbound from it?

trickybit

unread,
Feb 1, 2008, 2:53:42 AM2/1/08
to Android Developers
Run the RemoteService demo in the ApiDemos program, two client
activities to use startService() and bindService each, and you can
start, bind, unbind, stop, etc. and see how they interact.
> > > for the service to continue?- Hide quoted text -
>
> - Show quoted text -

hackbod

unread,
Feb 1, 2008, 4:17:45 AM2/1/08
to Android Developers
On Jan 31, 7:26 pm, jtaylor <juan.anthony.tay...@gmail.com> wrote:
> In using bindService, if there are no activities bound to the service,
> then the service stops running? If so, suppose I don't want that to
> happen. Suppose I want the service to keep running even when all
> activities that are bound to it are unbound from it?

If you want a service to run without any clients bound to it, you (or
the service) call startService() for it. Please, however, don't just
start a service and let it run forever because that is easier to do --
leaving a service running means the system needs to keep your process
around, which steals memory from others. You really should only have
a service running when it has something useful to do: retrieving data
from the network, playing music, etc.

Gabor Paller

unread,
Feb 1, 2008, 6:11:53 AM2/1/08
to Android Developers
There is one thing which was not evident for me reading the
documentation.
One can have a service that implements the startService() and the
bindService() lifecycle
at the same time. This service would have onStart, onCreate, onDestroy
and getBinder methods, would be
possible to start with both startService and bindService and if
started with startService, it will survive the
last client unbound from it. I have already implemented such a thing,
maybe I will write an example program
about it.

jtaylor

unread,
Feb 1, 2008, 8:14:02 PM2/1/08
to Android Developers
Thanks for the warning on letting a service run on.

jtaylor

unread,
Feb 1, 2008, 8:14:43 PM2/1/08
to Android Developers
Thanks, I guess that makes sense.

Gabor Paller

unread,
Feb 3, 2008, 5:13:53 AM2/3/08
to Android Developers
I created the example program.
http://mylifewithandroid.blogspot.com/2008/02/double-life-of-service.html
There are some interesting implications, however, on stopping the
service.
If a service is started and bound at the same time, it cannot be
stopped.

Reply all
Reply to author
Forward
0 new messages