4

I have a service that stores a Messenger as a member variable and returns messenger.getBinder() on onBind(Intent). I am connecting to this service with:

void Bind()
{
    Intent intent = new Intent("com.example.RemoteBindingService");
    bindService(intent, mServiceConnection,
            Context.BIND_AUTO_CREATE); // Context.BIND_AUTO_CREATE
                                       // means
                                       // "start if not started"

    mBound = true;
}

Before calling Bind(), sending a Message to the service does nothing. After calling Bind(), sending a Message to the service works correctly. However, I would expect that after calling

void Unbind()
{
    if(mBound == true)
    {
        unbindService(mServiceConnection);
        mBound = false;
    }
}

that sending a Message to the service would again do nothing. However, this is not the case - the service keeps working. Can anyone explain how to properly disconnect from a service?

David Doria
  • 9,873
  • 17
  • 85
  • 147

1 Answers1

0

I assume you started your Service with startService(), because if it would be a bound Service it would get killed if the last Activity unbounds from the Service.

You can stop it

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • I did not call startService(). So you're saying if something else is bound to it, even if I unbindService the service will still receive my messages? – David Doria Oct 02 '13 at 12:05
  • Yes, the service will be up until the last Activity/whatever unbounds. – Steve Benett Oct 02 '13 at 12:07
  • Can I call stopService even if I didn't call startService to disconnect everyone? – David Doria Oct 02 '13 at 12:09
  • I never tried that. But it should work. Please tell me what the result was. – Steve Benett Oct 02 '13 at 12:11
  • It's not possible as mentioned [here](http://stackoverflow.com/questions/2176375/android-service-wont-stop). – Steve Benett Oct 02 '13 at 12:17
  • When debugging this kind of app, is there a UI way (i.e. through the Android Settings or similar) to stop a service? In this case, I wouldn't expect anything else to be bound to this service, but it is behaving as if something is. I'd like to "force stop" the service so I can restart it the way I want (using only bind). – David Doria Oct 02 '13 at 12:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38482/discussion-between-steve-benett-and-david-doria) – Steve Benett Oct 02 '13 at 12:24