title: How to Interface onReceive Method
body: <p>I've set up a class called <code>NetworkStatus</code> that monitors the network status of the device. Within <code>NetworkStatus</code> I've defined a <code>BroadcastReceiver</code> that monitors if there has been a connectivity change, i.e., if the internet has been switched <em>off</em> or <em>on</em>. I want to 'expose' this method (i.e. <code>onReceive</code>) to the activity that instantiated the instance of <code>NetworkStatus</code>.</p>
  
  <p>I'm trying to achieve this by setting up an <code>interface</code> in the <code>NetworkStatus</code> class, but I'm not sure how to call the interface from the <code>onReceive</code> method of the <code>BroadcastReceiver</code> i.e.,</p>
  
  <pre><code>public class NetworkStatus {
      public static interface NetworkStatusCallbacks {
          void onReceive();
      }
  
      public static class ConnectivityChange extends BroadcastReceiver {
  
          public void onReceive(Context context, Intent intent) {
  
              // need to call NetworkStatusCallbacks onReceive here
          }
      }
  
  }
  </code></pre>
  
  <p>Then in the main activity I would do something like,</p>
  
  <pre><code>public class MyActivity extends Activity implements NetworkStatus.NetworkStatusCallbacks {
  
      @Override
      public void onReceive() {
        // Do stuff here
      }
  }
  </code></pre>
  
  <p>Would really appreciate some pointers. Thanks.</p>
  
  <p><strong>Possible Solution</strong></p>
  
  <p>Think I have found a solution. As Selvin pointed out, to do this requires the <code>Activity</code>. I therefore obtained the <code>Activity</code> by casting it from the activity context that is passed to a constructor for the <code>NetworkStatus</code> class, i.e., setting a constructor for the class as</p>
  
  <pre><code>private static nsc NetworkStatusCallbacks;
  public NetworkStatus (Context context) {
      Activity activity = (Activity) context;
      nsc = (NetworkStatusCallbacks) activity;
  }
  </code></pre>
  
  <p>I can then call the interface from the <code>onReceive</code> method of the <code>BroadcastReceiver</code> as follows:</p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {
      nsc.onReceive();
  }
  </code></pre>