Using both startService and bindService is handy to wait for the
onServiceConnected callback before requesting a singleton reference
that is set in the service's onCreate.
If you do this just after the startService, the reference will be
null.
The startService is needed for the service to continue running when
the activities have been destroyed (and call unbind).
A use case is a service managing a background XMPP connection in a
chat/social application (only the "sign out" user action would call
stopService).
So, Mark I don't understand your point when you say not to use both
startService and bindService.

On 19 juil, 13:47, Mark Murphy <mmur...@commonsware.com> wrote:
> Yossi wrote:
> > Hi Mark,
>
> > My code is relatively simple
>
> > public void onResume()
> > {
> >    Intent intent = new Intent(this, TrackingService.class);
> >    startService(intent);
> >    bindService(intent, _connection, Context.BIND_AUTO_CREATE);
> > }
>
> Do either startService() or bindService(), not both.
>
>
>
> > public void onPause()
> > {
> >    if(shouldStopService())
> >    {
> >            unbindService(_connection);
> >            stopService(new Intent(this, TrackingService.class));
> >    }
> > }
>
> Do either unbindService() or stopService(), matching up with what you
> chose to do in onResume().
>
> Unless your service is in some other process (which the use of
> TrackingService.class suggests it is not), you probably do not want to
> use bindService()/unbindService(). Use startService() to start it, then
> use stopService() only when you want to stop the service. Since it is in
> your process, use other means (e.g., static singleton reference to the
> service) to communicate with it from the activity.
>
> Right now, your service stop shortly after your activity goes
> off-screen, because you are telling the service to stop in onPause().
>
> http://developer.android.com/reference/android/app/Service.html#Servi...
>
> --
> Mark Murphy (a Commons 
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> Android 1.5 Programming Books:http://commonsware.com/books.html
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to