title: How can I catch the event of wifi state changes
body: <p>I am developing a Power Control Widget, in which I have an ImageButton that when pressed toggles the Wifi and also changes the button's src for visual confirmation. The problem is that I do not know how to detect when the Wifi has been disabled or enabled from other sources, like from the settings or from other power control widget, and change the button's src accordingly.</p>
  
  <p>For example, if I have both my widget and the Android default Power Control Widget on the home screen and I disable the Wifi using my widget, then the Wifi button of the Android default Power Control Widget gets disabled as well, but if I disable the wifi using the stock Power Control Widget, my wifi button's src does not change and still indicates that the wifi is enabled.</p>
  
  <p>Any ideas are appreciated as I can not find a solution to this.</p>
  
  <p><strong>EDIT:</strong>
  Here is my BroadcastReceiver:</p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {
  
      RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
      wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      int state = wifi.getWifiState();
      switch(state) {
          case WifiManager.WIFI_STATE_DISABLED:
              remoteViews.setImageViewResource(R.id.widget_wifi, R.drawable.ic_widget_wifi_off);
              break;
          case WifiManager.WIFI_STATE_ENABLED:
              remoteViews.setImageViewResource(R.id.widget_wifi, R.drawable.ic_widget_wifi_on);
              break;
          case WifiManager.WIFI_STATE_DISABLING:
              remoteViews.setImageViewResource(R.id.widget_wifi, R.drawable.ic_widget_wifi_off);
              break;
          case WifiManager.WIFI_STATE_ENABLING:
              remoteViews.setImageViewResource(R.id.widget_wifi, R.drawable.ic_widget_wifi_on);
              break;
      }           
  
  
  }
  </code></pre>
  
  <p>Also the AndroidManifest.xml:</p>
  
  <pre><code>&lt;receiver
  android:name="WidgetIntentReceiver"
      android:label="widgetBroadcastReceiver" &gt;
      &lt;intent-filter&gt;             
          &lt;action android:name="android.net.wifi.WIFI_STATE_CHANGED" /&gt;
          &lt;action android:name="android.net.wifi.STATE_CHANGE" /&gt;             
  &lt;/intent-filter&gt;
  </code></pre>
  
  <p>and the permissions:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"&gt;&lt;/uses-permission&gt;  
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"&gt;&lt;/uses-permission&gt; 
  &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE"&gt;&lt;/uses-permission&gt;   
  &lt;uses-permission android:name="android.permission.WAKE_LOCK"&gt;&lt;/uses-permission&gt;
  &lt;uses-permission android:name="android.permission.WRITE_SETTINGS"&gt;&lt;/uses-permission&gt;
  </code></pre>