Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly unbind services

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 :

  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?

  2. When the onDestroy() method is currently called and I call unbindService(), the Service's onUnbind() method isn't triggered. Why?

like image 250
Raphael Royer-Rivard Avatar asked Apr 08 '14 14:04

Raphael Royer-Rivard


People also ask

How do you bind a service?

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.

What is a bound 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.

How do I find my service instance android?

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?

How to bind and unbind protocols in a service?

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.

How do I unbind a client from a 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.)

How do I unbind a service from the certificate?

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?

Why do I get illegalargumentexception when I bind and unbind a service?

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.


1 Answers

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.

like image 87
kiruwka Avatar answered Oct 20 '22 23:10

kiruwka