I have a Service that is binded with BIND_AUTO_CREATE to my Activity and is started with START_STICKY. I do not explicitly call startService().
Since the onDestroy() method is not always called when my Activity get killed and the onStop() method is not viable since I don't want my Service to stop when the user simply presses the Home button, I don't know when to unbind my Service.
Here are my questions :
If I want my Service to run when my Activity is alive and to stop when my Activity get killed, where should I unbind my Service?
When the onDestroy() method is currently called and I call unbindService(), the Service's onUnbind() method isn't triggered. Why?
To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.
A bound service is like a server in a client-server interface. A bound server allows components, such as activities, to bind to the service, send requests, receive responses and even perform IPC or Inter-Process Communication.
Use both startService() and bindService() , if needed. How can I get an instance to the service started if I do not use bindService? Either use bindService() with startService() , or use a singleton. By "using a singleton" you mean that I should declare my methods static in the service class?
Now, to bind or unbind protocols to services, first click the connection that you want to modify. Then, under Bindings, check or uncheck the options. Checked is bound, unchecked is unbound. As you can see, you can independently bind and unbind protocols like TCP/IPv6 and TCP/IPv4 from any registered network service.
To disconnect from the service, call unbindService () . When your client is destroyed, it will unbind from the service, but you should always unbind when you're done interacting with the service or when your activity pauses so that the service can shutdown while its not being used. (Appropriate times to bind and unbind is discussed more below.)
Unfortunately, you can’t unbind the service from the certificate. Instead, you have to re-assign the services to another certificate first. After that, you can remove the certificate. Did you enjoy this article?
Binding and Unbinding a service with different Context. calling unBind (mserviceConnection) more than bind (...) First point is self explanatory. Lets explore the second source of error more deeply. Debug your bind () and unbind () calls. If you see calls in these order then your application will end up getting the IllegalArgumentException.
I do not explicitly call startService()
In that case it doesn't make sense to override onStartCommand
(and return START_STICKY
) as it won't be called.
1.If I want my Service to run when my Activity is alive and to stop when my Activity get killed, where should I unbind my Service?
if you don't want to do it in onPause
, you can unbind in onDestroy
, that is fine. In a rare situation when your activity gets killed without onDestroy
it will be unbound by Android, (so your service will be unbound & destroyed properly too), as stated here :
When your client is destroyed, it will unbind from the service
As for
2.When the onDestroy() method is currently called and I call unbindService(), the Service's onUnbind() method isn't triggered. Why?
I suggest that you have someone else bound to it, otherwise onUnbind
should be called. Try putting a breakpoint in this method and verify it gets hit. Also, verify that your service onDestroy
is called in this situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With