7

The original bind/unbind service can be called by client using bindService()/unbindService().

My question is how to unbind service in service side, not called unbindService() by client, probably I should call it unbindClient.

I think the service should know which clients are bound to it, so is there any way to tell the service to unbind a specific client?

Because i only write the service, and i do't know if the client called unbindService() correctly,so i have this question..

DerApe
  • 3,097
  • 2
  • 35
  • 55
kwf2030
  • 195
  • 2
  • 13
  • 1
    There might be something you can do to restructure your classes. Why do you need to know which client disconnected from the service? – Matthew Quiros Sep 21 '12 at 03:30
  • like normal c/s program,i want to do some underlying recycle work when a client disconnected,do you have any better idea? – kwf2030 Sep 22 '12 at 09:17
  • Can you call it from the `Activity` that disconnected from the `Service` instead? There's an `onDestroy()` method for activities, or `onPause()` or `onStop()`. You might be able to put it there. – Matthew Quiros Sep 22 '12 at 09:52

3 Answers3

1

A service cannot unbind itself. You can make a started service stop itself with stopSelf() though.

The OS keeps track of all the binding and unbinding behind the scenes. You don't have to worry about object recycling, Android does it for you.

Sam
  • 11,799
  • 9
  • 49
  • 68
  • 1
    If you think the answer is misleading. Please point out what do you think is wrong with it. – Sam Jul 19 '13 at 15:38
0

If you have access to client code -

You can call startService() from the client - which will start the service (and not end the Service on exiting the Activity - since only stopService() will close it).

And also, Client could call bindService() - if they want to perform Activity-Service interaction.

On exiting/unbinding the client, the service will still run since startService() was called - Service can perform other operations and then call stopService() which will destroy itself.

Sufian
  • 6,405
  • 16
  • 66
  • 120
sarnabtech
  • 486
  • 3
  • 12
0

If you want to merely limit the functionality that you service returns once it is started and bindService() is called.You could register a client and unregister when you are done with the service.That way binding your service will not have any impact.This I picked from the Remote Messenger Service example in the android website

Droidekas
  • 3,464
  • 2
  • 26
  • 40