Question

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.

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.

Any ideas are appreciated as I can not find a solution to this.

EDIT: Here is my BroadcastReceiver:

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;
    }           


}

Also the AndroidManifest.xml:

<receiver
android:name="WidgetIntentReceiver"
    android:label="widgetBroadcastReceiver" >
    <intent-filter>             
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        <action android:name="android.net.wifi.STATE_CHANGE" />             
</intent-filter>

and the permissions:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>   
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Was it helpful?

Solution

Listen to System broadcasts such as ACCESS_NETWORK_STATE or ACCESS_WIFI_STATE by registering a BroadcastReceiver in your manifest.

To listen to these system broadcast you need permissions declared in your manifest. Refer to this documentation for the broadcasts actions and the corresponding permissions required.

I hope you know how to implement a broadcast receiver.

OTHER TIPS

public boolean checkOnlineState() {
        ConnectivityManager CManager =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo NInfo = CManager.getActiveNetworkInfo();
        if (NInfo != null && NInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top