How to refer to the application context from a broadcast receiver
<p>I'm using the usual technique of extending Application in order to store global constants. </p>

<p>So within my activities, I can simply do (in oncreate()):</p>

<pre><code>W = (WcmApplication) getApplicationContext();
</code></pre>

<p>However, this doesn't work for broadcast receivers:</p>

<pre><code>The method getApplicationContext() is undefined for the type MyReceiver
</code></pre>

<p>So, thinking I was being clever, I tried to do:</p>

<pre><code> W = (WcmApplication) context;
</code></pre>

<p>... but that throws an error at runtime saying my broadcast receiver is not allowed to access that context</p>

<p>Not giving up, I try this:</p>

<pre><code>W = (WcmApplication) Context.getApplicationContext();
</code></pre>

<p>... no dice</p>

<p>So I ended up having to do:</p>

<pre><code>W = (WcmApplication)context.getApplicationContext() ;
</code></pre>

<p>... and that works nicely, however I have no idea why. </p>

<p>Can someone explain why one works and not the others?</p>

<p>Thank you!</p>
