开发者

onReceive called when it shouldn't

开发者 https://www.devze.com 2023-02-04 10:14 出处:网络
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 proc

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消