Android Service questions about bind/unbind?
Asked Answered
C

3

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..

Coated answered 21/9, 2012 at 3:10 Comment(3)
There might be something you can do to restructure your classes. Why do you need to know which client disconnected from the service?Partlet
like normal c/s program,i want to do some underlying recycle work when a client disconnected,do you have any better idea?Coated
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.Partlet
H
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.

Huehuebner answered 21/3, 2013 at 7:29 Comment(1)
If you think the answer is misleading. Please point out what do you think is wrong with it.Huehuebner
B
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.

Bongbongo answered 19/3, 2013 at 23:4 Comment(0)
P
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

Prosaism answered 7/8, 2014 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.