简体   繁体   English

如何捕获wifi状态更改事件

[英]How can I catch the event of wifi state changes

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. 我正在开发一个电源控制小部件,其中有一个ImageButton,按下该按钮可切换Wifi,还可以更改按钮的src以进行视觉确认。 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. 问题是我不知道如何检测何时从其他来源(例如从设置或其他电源控制小部件)禁用或启用了Wifi,并相应地更改按钮的src。

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. 例如,如果我在主屏幕上同时拥有我的窗口小部件和Android默认的Power Control窗口小部件,并且使用该窗口小部件禁用了Wifi,那么Android默认Power Control窗口小部件的Wifi按钮也会被禁用,但是如果我禁用了wifi使用库存的电源控制小组件,我的wifi按钮的src不会更改,并且仍指示已启用wifi。

Any ideas are appreciated as I can not find a solution to this. 感谢您提出任何想法,因为我找不到解决方案。

EDIT: Here is my BroadcastReceiver: 编辑:这是我的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: 还有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>

Listen to System broadcasts such as ACCESS_NETWORK_STATE or ACCESS_WIFI_STATE by registering a BroadcastReceiver in your manifest. 听系统广播诸如ACCESS_NETWORK_STATEACCESS_WIFI_STATE注册一个BroadcastReceiver在您的清单。

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. 希望您知道如何实现广播接收器。

public boolean checkOnlineState() {
        ConnectivityManager CManager =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo NInfo = CManager.getActiveNetworkInfo();
        if (NInfo != null && NInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM