title: WiFi Broadcast Receiver for 2 different classes, how do I cast my activities properly?
body: <p>I am following the <a href="http://developer.android.com/guide/topics/connectivity/wifip2p.html" rel="nofollow">WiFi direct guide for Android</a> because I am building an app that uses WiFi direct. I also have 2 classes; one is called ClientReceive and one is called HostSend. The Client initiates a connection to the host, and then the host will push data to the client.</p>
  
  <p>I have made a broadcast receiver that listens for state changes when I begin to start my WiFi direct setup:</p>
  
  <pre><code>package com.mgamerzproductions.wifidirecttest;
  
  import android.app.Activity;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.net.NetworkInfo;
  import android.net.wifi.p2p.WifiP2pDevice;
  import android.net.wifi.p2p.WifiP2pManager;
  import android.net.wifi.p2p.WifiP2pManager.Channel;
  import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
  import android.util.Log;
  
  /**
   * A BroadcastReceiver that notifies of important wifi p2p events.
   */
  public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
  
      private WifiP2pManager manager;
      private Channel channel;
      private WiFiActivity activity;
  
      /**
       * @param manager WifiP2pManager system service
       * @param channel Wifi p2p channel
       * @param Host class (HostSend) for this constructor.
       */
      public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
              HostSend activity) {
          super();
          this.manager = manager;
          this.channel = channel;
          this.activity = activity;
      }
  
      /**
       * @param manager WifiP2pManager system service
       * @param channel Wifi p2p channel
       * @param activity activity associated with the receiver
       */
      public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
              ClientReceive activity) {
          super();
          this.manager = manager;
          this.channel = channel;
          this.activity = activity;
      }
  
      /*
       * (non-Javadoc)
       * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
       * android.content.Intent)
       */
      @Override
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
  
              // UI update to indicate wifi p2p status.
              int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
              if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                  // Wifi Direct mode is enabled
                  activity.setIsWifiP2pEnabled(true);
              } else {
                  activity.setIsWifiP2pEnabled(false);
                  //activity.resetData();
  
              }
              Log.d(Launch.APPTAG, "P2P state changed - " + state);
          } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
  
              // request available peers from the wifi p2p manager. This is an
              // asynchronous call and the calling activity is notified with a
              // callback on PeerListListener.onPeersAvailable()
              if (manager != null) {
                  /*manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
                          .findFragmentById(R.id.frag_list));*/
              }
              Log.d(Launch.APPTAG, "P2P peers changed");
          } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
  
              if (manager == null) {
                  return;
              }
  
              NetworkInfo networkInfo = (NetworkInfo) intent
                      .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
  
              if (networkInfo.isConnected()) {
  
                  // we are connected with the other device, request connection
                  // info to find group owner IP
  
                  HostSend connectionListener = activity;
                  manager.requestConnectionInfo(channel, connectionListener);
              } else {
                  // It's a disconnect
                  activity.resetData();
              }
          } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
              /*DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
                      .findFragmentById(R.id.frag_list);
              fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
                      WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));*/
  
          }
      }
  }
  </code></pre>
  
  <p>However, I have another class, ClientReceive, that also needs a receiver because both devices need to setup their WiFi direct and use callbacks (I think that's what these are called? Callbacks to the activity?) to set up a connection. Both classes are a subclass of WiFi activity, which is essentially an empty class that overrides a few methods in Activity with just super.methodname() so the children can access it. IT has some extra methods related to WiFi. I created this so I could avoid casting, but it doesn't work cause the children classes have different methods and if I use WiFiActivity instead of it's subclasses I can't use the subclasses methods.</p>
  
  <p>Is there a way to do this properly? My code is a work in progress (which is why it's kind of ugly) but I'm out of ideas. I have 2 constructors, one from each activity on each device (one is host device, one is client device). What I want to do is share this class between both of the classes but I don't know how to cast the HostSend connectionListener (HostSend) activity properly because I don't know how to cast items like this.</p>
  
  <p>Thanks. Sorry for the long winded explanation.</p>