1

我在 Android Devlopoers Forum 上使用了可用的代码。我的活动是:

protected void onCreate(Bundle savedInstanceState){
      mIntentFilter = new IntentFilter();
      mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
      mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
      mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
      mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

      mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
      mChannel = mManager.initialize(this, getMainLooper(), null);
      mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);  
}

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(mReceiver, mIntentFilter);
}
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mReceiver);
}

我有一个接收器类:

public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
            if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                Toast.makeText(this, "Enabled", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Disabled", Toast.LENGTH_LONG).show();
            }
 }

但它不起作用。没有错误,Toast 也没有显示。我想我必须启用 Wifi-Direct。在这方面需要帮助

我的清单文件:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4

0 回答 0