Question

some seconds ago I was debugging my widget and I've found that the onReceive method is called even if my widget isn't on the home screen (it listens for WIFI_STATE_CHANGED_ACTION ). Even if the process of my widget isn't running, it will be started and the method called. The code in the manifest is the following:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">

    <receiver android:name="WiFiWidget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>

        <meta-data android:name="android.appwidget.provider" 
                   android:resource="@xml/widget_info" />
    </receiver>

</application>

The method onReceive simply calls another method toggleState ( to change the wifi state ) and then updates the widget image ( from off to on image or viceversa ).

Obviously the call of the two methods toggleState and updateWidget ( and so the complete onReceive ) are useless if the widget isn't actually active on the home screen..

So, I want my onReceive called only if my widget is effectively placed on home screen..there's a way to do this? :)

thanks in advance for the answers

Was it helpful?

Solution

Why wouldn't your broadcast receiver be called? That's the point of a broadcast receiver is to be called when your process/activity isn't running.

If you only want it called at certain times, you have to register it in code in your widget, and unregister it when you don't want to receive broadcasts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top