title: WIFI_STATE_CHANGED_ACTION intent not received upon connection to WiFi access point?
body: <p>Part of my app's functionality is to scan and display a list of WiFi access points, and then connect to the one chosen by the user. I have this functionality working. Now, I also wish to be notified when the connection "goes through". This should be fairly simple, but I find myself stumbling. </p>
  
  <p>I have read through various posts here at SO, and they all mention registering for <code>WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION</code> or <code>WifiManager.WIFI_STATE_CHANGED_ACTION</code>. However, neither of these works for me. Can anyone spot any mistake in this code? (I am leaving out the parts which do the scan and stuff)</p>
  
  <p><em><strong>Expected behavior:</em></strong> As soon as the connection is successful (i.e, when I see the "connected" icon on the notification bar), the broadcast should be received and I should see the toast.</p>
  
  <p><em><strong>Observed behavior:</em></strong> The broadcast is received when the app first starts, and whenever I switch back to it (i.e, whenever <code>onResume()</code> is called; or I suspect, whenever I register for the intent)</p>
  
  <pre><code>public class WifiScanActivity extends Activity {
  
      WifiManager mainWifi;
      WifiReceiver mWifiReceiver;
      IntentFilter mIntentFilter;
      private final static String TAG = "WifiScanActivity";
      public static final String INTENT_FOR_WIFI_CONNECTED =
              WifiManager.WIFI_STATE_CHANGED_ACTION;
      // public static final String INTENT_FOR_WIFI_CONNECTED =
      // WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
          mWifiReceiver = new WifiReceiver();
          mIntentFilter = new IntentFilter();
          mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
          mIntentFilter.addAction(INTENT_FOR_WIFI_CONNECTED);
          registerReceiver(mWifiReceiver, mIntentFilter);
          mainWifi.startScan();
      }
  
      protected void onPause() {
          unregisterReceiver(mWifiReceiver);
          super.onPause();
      }
  
      protected void onResume() {
          registerReceiver(mWifiReceiver, mIntentFilter);
          super.onResume();
      }
  
      class WifiReceiver extends BroadcastReceiver {
  
          public void onReceive(Context c, Intent intent) {
              Log.d(TAG,
                  "In WifiReceiver: Broadcast Received = " + intent.getAction());
              if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent
                  .getAction())) {
                  // Display the ListView and connect to the selected AP
              } else if (INTENT_FOR_WIFI_CONNECTED.equals(intent.getAction())) {
                  if (WifiManager.WIFI_STATE_ENABLED == intent.getIntExtra(
                      WifiManager.EXTRA_WIFI_STATE, 0)) {
                      displayNetworkInfo();
                  }
                  /*if(true == intent.getBooleanExtra(
                   * WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
                   *  displayNetworkInfo();
                  }*/
              }
          }
      }
  
      private void displayNetworkInfo() {
          WifiInfo wifiInfo = mainWifi.getConnectionInfo();
          String ssid = wifiInfo.getSSID();
          int ip = wifiInfo.getIpAddress();
          String message = "Connection established.\nSSID = " + ssid
              + "; IP Address = " + Helper.displayIpAddress(ip);
          Log.d(TAG, message);
          Toast.makeText(this, message, Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>
  
  <p>If I uncomment the code for WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION, I don't see the broadcast being received at all.</p>
  
  <p><em><strong>Note</em></strong>: I know that the connection is successful because I see the status in Android's wifi settings screen.</p>