A Real Use for getApplicationContext()!
getApplicationContext() is a seriously overused method.
Most times where I see it, using
this would work just as
well, if not better.
One case where
getApplicationContext() can be useful,
though, lies in the intersection of sticky broadcasts and
BroadcastReceiver, as discovered through discussions
on this StackOverflow question.
A sticky broadcast is one where Android holds onto the last
Intent that was broadcast for that action. You can get that
Intent by calling
registerReceiver(), with an
that matches the action, but a
Things get tricky, though, if you try doing that from
onReceive() method of a manifest-registered
such as an app widget implementation. If you use the
object that is passed into
onReceive() and call
on it, you get an error. The error complains about a
BroadcastReceiver, despite the fact that,
in this case, you’re actually just trying to get a sticky
The workaround, as Ms. Hackborn pointed out, is to use
Context object will
registerReceiver() call, so you can get the
Intent value you seek.