I have a widget that I am trying to use to enable / disable Wi-Fi and also to display additional information. I want to show a transient hourglass icon when wifi is on / off, so I listen to the android.net.wifi.WIFI_STATE_CHANGED intent.

When this intent is caught, I call the widget onUpdate function, in which case I have this block of code to change the displayed image:

WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMan.getConnectionInfo();

RemoteViews views = new RemoteViews("com.nickavv.cleanwidgets", R.layout.wifi_toggle_layout_large);

supState = wifiInfo.getSupplicantState();
wifiState = wifiMan.getWifiState();

if(wifiState == WifiManager.WIFI_STATE_ENABLED) {
    //Show enabled image
} else if(wifiState == WifiManager.WIFI_STATE_DISABLED){
    //Show disabled image
} else {
    //Show hourglass


And here is my code for catching clicks and wifi state changes:

public void onReceive(Context context, Intent intent) {  
    super.onReceive(context, intent);
    if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { 
        if(wifiState != WifiManager.WIFI_STATE_ENABLING || wifiState != WifiManager.WIFI_STATE_DISABLING) {
    AppWidgetManager myAWM = AppWidgetManager.getInstance(context);
    ComponentName cn = new ComponentName(context, WifiWidgetLarge.class);
    onUpdate(context, myAWM, myAWM.getAppWidgetIds(cn));


This is great for a smaller widget that is just a toggle switch, but on a larger widget, the same code does not produce the correct results. It shows an hourglass when turned off, but not during turn on. It looks like the intent is received too late to display anything, as the debug logs show that they are eventually accepted. It just doesn't go into WIFI_STATE_ENABLING until the state changes to WIFI_STATE_ENABLED.

Most puzzling is everything, the widget works great on my tablet and my cell phone just not on my phone. However, I don't want to release it until I can be more confident in its performance. Any ideas?


source to share

1 answer

There may have been more tweaks after the Gingerbread version. You can now check the android version and if it is GB you will need a little hack.

You can check WIFI_STATE_CHANGED_ACTION and get previous / next states using




All Articles