5

For communicating between the application and a Service, why would I use a Bound service rather than sending data in the Intent:

mServiceIntent = new Intent(getActivity(), RSSPullService.class);
mServiceIntent.setData(Uri.parse(dataUrl));

I read that "If the service is already running, it will be called with onStartCommand() again, to deliver the new Intent, but a second copy is not created." Which means that I Could send messages in that intent to affect the service's progress, this is what is done in the google RandomMusicPlayer example:

public void onClick(View target) {
    // Send the correct intent to the MusicService, according to the 
    // button that was clicked
    if (target == mPlayButton)
        startService(new Intent(MusicService.ACTION_PLAY));
    else if (target == mPauseButton)
        startService(new Intent(MusicService.ACTION_PAUSE));
    else if (target == mSkipButton)
        startService(new Intent(MusicService.ACTION_SKIP));
    else if (target == mRewindButton)
        startService(new Intent(MusicService.ACTION_REWIND));
    else if (target == mStopButton)
        startService(new Intent(MusicService.ACTION_STOP));
    else if (target == mEjectButton) {
        showUrlDialog();
}
Adam K Dean
  • 7,387
  • 10
  • 47
  • 68
serenskye
  • 3,467
  • 5
  • 35
  • 51

1 Answers1

0

There are a number of reasons for binding to a Service as opposed to sending it asynchronous messages. One important reason is it gives you more control of the lifetime of a service. If you are simply sending intents that are handled by the service, the service may well go away -- losing any internal state -- between messages. Bound services receive special treatment when Android is looking for resources that can be freed.

Another, unrelated, reason is if you are binding to an in-process service you can cast the IBinder to a known class and call methods on it directly. This provides a very rich (although tightly coupled) interface to the service. It would be difficult to simulate this rich interaction using message passing via Intents.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
  • "If you are simply sending intents that are handled by the service, the service may well go away -- losing any internal state -- between messages. " I'm not sure that is true, the documentation says "The Android system will attempt to keep the process hosting a service around as long as the service has been started OR has clients bound to it" – serenskye Feb 21 '13 at 10:06
  • Also see this [post](http://stackoverflow.com/questions/5388915/can-started-and-bound-services-live-forever?rq=1) that explains it nicely – serenskye Feb 21 '13 at 10:49
  • @serenskye: A service is always eligible for garbage collection. However if it is bound it is much less likely to be garbage collected than it is if it's running because it was started at some time in the past. – Dale Wilson Mar 01 '13 at 18:36
  • On the other hand a service that was previously bound and becomes unbound goes away immediately. Which gets me back to the statement "[binding] gives you more control of the lifetime of a service." This link: http://developer.android.com/guide/components/services.html explains it well. – Dale Wilson Mar 01 '13 at 18:38