title: Android Broadcastreceiver with listener
body: <p>I have a Broadcastreceiver for monitoring network state. When it changes I want to send a custom Exception to my BaseActivity in order that can be showed in any Activity.</p>
  
  <p>To send the exception I have a listener, which is implemented on my BaseActivity.</p>
  
  <p>BroadcastReceiver:</p>
  
  <pre><code>public class ConnectionStateReceiver extends BroadcastReceiver {
      private ConnectionStateListener connectionStateListener;
  
      public void onReceive(Context context, Intent intent) {
          this.connectionStateListener = (ConnectionStateListener) context;
  
          if (this.connectionStateListener != null) {
              this.connectionStateListener.showConnectionStateLostException(new NetworkConnectionException());
          }
          ......
  </code></pre>
  
  <p>Listener:</p>
  
  <pre><code>public interface ConnectionStateListener {
      void showConnectionStateLostException(Exception exception);
  }
  </code></pre>
  
  <p>BaseActivity:</p>
  
  <pre><code>public abstract class BaseActivity extends ActionBarActivity implements ConnectionStateListener, BatteryChangeListener {
  
  ...
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      this.ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
      this.batteryStatus = this.registerReceiver(null, ifilter);
      //Receiver called when battery level changes
      this.systemBatteryLevelReceiver = new BatteryChangeReceiver();
  
      registerReceiver(systemBatteryLevelReceiver, ifilter);
  
  }
  
  ...
  @Override
  public void showConnectionStateLostException(Exception exception) {
      showSystemStateMessage(ExceptionMessageUtils.createSystemExceptionMessage(this, exception));
  }
  
  @Override
  public void showChangeBatteryException(Exception exception) {
      showSystemStateMessage(ExceptionMessageUtils.createSystemExceptionMessage(this, exception));
  }
  ...
  </code></pre>
  
  <p>Manifest:</p>
  
  <pre><code>...
  &lt;receiver android:name=".data.broadcastreceivers.ConnectionStateReceiver"
          android:enabled="false"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE"/&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  </code></pre>
  
  <p>The problem is when it trys to do the cast on the receiver:</p>
  
  <pre><code>this.connectionStateListener = (ConnectionStateListener) context;
  </code></pre>
  
  <p>It gives me the following error:</p>
  
  <pre><code>01-14 11:45:38.347    8766-8766/com.massfactory.mobile.android.stlparatransit E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.massfactory.mobile.android.stlparatransit, PID: 8766
  java.lang.RuntimeException: Unable to start receiver com.massfactory.mobile.android.stlparatransit.data.broadcastreceivers.ConnectionStateReceiver: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to com.massfactory.mobile.android.stlparatransit.domain.broadcastlisteners.ConnectionStateListener at android.app.ActivityThread.handleReceiver(ActivityThread.java:2593) at android.app.ActivityThread.access$1700(ActivityThread.java:148)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5274)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
   Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to com.massfactory.mobile.android.stlparatransit.domain.broadcastlisteners.ConnectionStateListener
          at com.massfactory.mobile.android.stlparatransit.data.broadcastreceivers.ConnectionStateReceiver.onReceive(ConnectionStateReceiver.java:37)
          at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
          at android.app.ActivityThread.access$1700(ActivityThread.java:148)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5274)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  </code></pre>
  
  <p>I have another broadcast wich is monitoring battery state with the same "structure" and doesn't give any problem, the only diference is that the "battery broadcast" is declared dinamically (not in the manifest as this one).</p>
  
  <p>Battery Listener:</p>
  
  <pre><code>public interface BatteryChangeListener {
     void showChangeBatteryException(Exception exception);
  }
  </code></pre>
  
  <p>Any idea why is happening this and what I should do to fix it?</p>
  
  <p>Thanks in advance,</p>