The big difference between the application context and an activity context
or service context is lifecycle.  The activity context is valid until the
activity's onDestroy(); service until the service's onDestroy(); and
Application is never destroyed.

If you have bound to a service, and the context you used for that is
destroyed, then the framework will clean up that binding (printing a
warning message to the log about it being leaked).  So if you later try to
unbind, the context is already cleaned up, so you crash because the binding
doesn't exist.

This difference is the main thing driving you to decide whether to use the
application context or the activity/service context: if the thing you are
doing is supposed to be in...  well...  the context of the
activity/service, then that is the context you should use, and work
correctly with the lifecycle.  If you need to continue to have an active
context outside of the context of the activity/service, then you will want
the application context.

On Sun, Feb 5, 2012 at 7:52 PM, Zsolt Vasvari <zvasv...@gmail.com> wrote:

> Well, the unbind() call that threw the "Service not registered" was
> originally bound to from a WakefulIntentService, not an Activity.
>
> I bound to the service in doWakefulWork() and unbind() in onDestroy()
> as well as when I am done with the service.
>
> Basically I have a cleanup() method which looks like this:
>
> public synchronized void cleanup(Context context)
> {
>      if (this.serviceConnection != null)
>      {
>
> context.getApplicationContext().unbindService(this.serviceConnection);
>  //
> Used to be just context.unbindService()
>            this.serviceConnection = null;
>      }
> }
>
> I've never even once since this error when I used to use
> getApplicationContext() and now that I switched to just the
> WakefulIntentService Context, I got it twice -- once myself and once
> by a guy who's testing for me.
>
> I am now back to using the Application Context, thought I don't
> understand still why I have to.  After all, the Service Context should
> remain the same throughout the lifecycle of the Service.
>
>
>
>
> On Feb 6, 6:48 am, Mark Murphy <mmur...@commonsware.com> wrote:
> > On Sun, Feb 5, 2012 at 5:38 PM, Zsolt Vasvari <zvasv...@gmail.com>
> wrote:
> > > Well, I made my change, but one of the uses for the Context is bind to
> a
> > > service.
> >
> > > This now fails sometimes on unbind() with a "Service not registered"
> > > message.
> >
> > > I asuume I still have to use the Application Context to bind to
> services?
> >
> > "Have to" is a strong term. However, particularly for activities that
> you:
> >
> > -- want to have access to a bound service, and
> > -- want to allow Android to destroy and recreate on a configuration
> change
> >
> > then binding from Application is a good idea.
> >
> > http://commonsware.com/blog/2010/09/29/another-use-getapplicationcont...
> >
> > --
> > Mark Murphy (a Commons Guy)http://commonsware.com|
> http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
> >
> > _The Busy Coder's Guide to *Advanced* Android Development_ Version 2.4
> > Available!
>
> --
> 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
>



-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
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