EUREKA! I've figured it out based on some of the documentation I
missed. For those who also have trouble...

The docs for ContextWrapper.startService(Intent service) includes the
following line...
"Using startService() overrides the default service lifetime that is
managed by bindService(Intent, ServiceConnection, int): it requires
the service to remain running until stopService(Intent) is called,
regardless of whether any clients are connected to it."

This is the trick, simply run startService() before you attempt to
bind to it! So before, my onCreate contained:
        bindService(new Intent(MDService.class.getName()),
mConnection, Context.BIND_AUTO_CREATE);

Now this has been replaced by:
        Intent i = new Intent(MDService.class.getName());
        startService(i);
        bindService(i, mConnection, Context.BIND_AUTO_CREATE);

After that change, calling unbindService(mConnection) does NOT destroy
the service :)

So starting a service by binding it from an activity links the
service's life-cycle with that of the activity. While starting the
Service first, gives it it's own lifecycle, and you can still bind to
it right after. (And you still know the service will only actually be
started once.)

Also, doing this seems to have alleviated my 2nd problem that I
described in my 1st post, but this requires a little more testing
before i can confirm it.

On Nov 23, 3:09 pm, G <[EMAIL PROTECTED]> wrote:
> I just realized that the in API Demo for Remote Service Binding, the
> service is destroyed when the activity is destroyed as well. So it's
> no help in this case. Can anyone point to a code sample in which a
> service outlives it's binding in an activity? Do I need to use a
> BroadcastReceiver or something? I'm very confused :(
>
> On Nov 23, 2:46 pm, G <[EMAIL PROTECTED]> wrote:
>
> > That is what I've been trying to do, below is the service definition
> > in my AndroidManifest.xml file
>
> >     <service android:name=".MDService" android:process=":remote">
> >         <intent-filter>
> >                 <action 
> > android:name="com.episode6.android.carolla.MDService"></
> > action>
> >                 </intent-filter>
> >         </service>
>
> > And I bind the service in my activity with the following call...
>
> > bindService(new Intent(MDService.class.getName()), mConnection,
> > Context.BIND_AUTO_CREATE);
>
> > And whenever I run unbindService(mConnection); the service still gets
> > destroyed.
>
> > On Nov 23, 1:52 pm, Mark Murphy <[EMAIL PROTECTED]> wrote:
>
> > > G wrote:
> > > > 1) When my main activity is destroyed, my service is getting destroyed
> > > > along with it. The service's onDestroy get's called, playback stops,
> > > > the notification gets cleared. How can I avoid this?
>
> > > Have you tried making your service a remote service, one that runs in
> > > its own process?
>
> > >http://code.google.com/android/samples/ApiDemos/src/com/example/andro...
>
> > > I haven't tried a remote service yet myself, so I'm not 100% certain it
> > > will resolve this problem.
>
> > > --
> > > Mark Murphy (a Commons Guy)http://commonsware.com
>
> > > Android Training on the Ranch! -- Mar 16-20, 
> > > 2009http://www.bignerdranch.com/schedule.shtml
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to