0

As a developer who's new to android I think I've perhaps misunderstood the bound service.

I've created a service to wrap up access to a server. As part of this service, the service is listening to a multicast address to identify when devices on the local network appear and disappear.

However I'm having problems due to there being multiple instances of the service started. This happens when the activity is closed (using the back button) and then re-opened shortly after. The effect is that the service is unable to bind to the local port and so can not listen to the multicast.

I had thought that the bound service would die once the activity stopped but this appears not to be the case.

Have I misunderstood the bound service or am I doing something else wrong?

Edit

So it seams my misunderstanding was that I'd assumed the program would be cleaned up as soon as the last non-daemon thread was closed and that would be as soon as the activity terminated.

I've fixed this by explicitly cancelling the listening thread and closing the socket in onDestroy().

Philip Couling
  • 13,581
  • 5
  • 53
  • 85
  • Services are like singletons: http://stackoverflow.com/a/3692966/995891 - there is something else in your implementation that blocks the port, i.e. you are starting your thread more than once inside your service. (Also threads can continue to run without being referenced from other code, they are not magically garbage collected) – zapl Mar 25 '13 at 19:21

2 Answers2

1

However I'm having problems due to there being multiple instances of the service started

There will only ever be zero or one instance of a given service class running in your process at any time.

I had thought that the bound service would die once the activity stopped but this appears not to be the case.

The service will be destroyed sometime after:

  • All calls to bindService() have had their corresponding unbindService(), and
  • If you called startService() ever for this running service instance, you call stopService() or the service calls stopSelf()

The precise timing of the service being destroyed is indeterminate, though usually it is fairly quick from what I have seen. I cannot rule out the possibility, though, that your second activity instance binds to the service before it is destroyed, thereby preventing it from being destroyed.

or am I doing something else wrong?

Perhaps you are not properly cleaning up your multicast socket when the service is being destroyed, or perhaps you are trying to open the socket when it is already open from a previous binding.

I am not completely clear why you are using the binding pattern here. Personally, I find it to be more trouble than it is worth, usually. Perhaps consider using the command pattern, sending commands to the service via startService() and stopping the service via stopService() or stopSelf().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Try using Stopservice() on onbackpressed()

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64