2014-10-29 43 views
1

我在嘗試理解和實現「Creating P2P Connections with Wi-Fi」中描述的(不完整)代碼。這是作爲一個單獨的類添加到我現有的活動。示例代碼中有一行如下:什麼/ WiFiDirectActivity在哪裏

activity.setIsWifiP2pEnabled(true); 

但是不顯示變量「activity」來自哪裏。尋找它採用setIsWifiP2pEnabled其他示例代碼()我看到one which declared activity如下:

private WiFiDirectActivity activity; 

,但如果我再補充一點線我得到的錯誤WiFiDirectActivity cannot be resolved to a type。 Eclipse沒有建議添加任何導入。所以我被卡住了。

+0

WIFI直接的完整Android SDK中的樣品也有顯然是[完全完整的示例](https://github.com/Miserlou/Android-SDK-Samples/tree/master/WiFiDirectDemo)這對GitHub([指出](http://stackoverflow.com/questions/39132970/cannot (@androidXP)解析 - 符號 - 什麼是活動?noredirect = 1&lq = 1#comment65613163_39132970)。 – 2016-08-24 22:27:47

回答

0
/** 
* A BroadcastReceiver that notifies of important wifi p2p events. 
*/ 
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver { 

    private WifiP2pManager manager; 
    private Channel channel; 
    private WiFiDirectActivity activity; 

    /** 
    * @param manager WifiP2pManager system service 
    * @param channel Wifi p2p channel 
    * @param activity activity associated with the receiver 
    */ 
    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, 
      WiFiDirectActivity 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(WiFiDirectActivity.TAG, "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(WiFiDirectActivity.TAG, "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 

       DeviceDetailFragment fragment = (DeviceDetailFragment) activity 
         .getFragmentManager().findFragmentById(R.id.frag_detail); 
       manager.requestConnectionInfo(channel, fragment); 
      } 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)); 

     } 
    } 
} 

這裏是在github上 GITHUB

+0

雖然此鏈接可能會回答問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/13456884) – Soham 2016-08-25 09:36:57

+0

@Soham更新回答 – androidXP 2016-08-25 09:51:15