简体   繁体   English

wifi状态更改时,如何调用小部件更新?

[英]How do you call a widget update when the wifi status changes?

I have a wifi widget that toggles the wifi state. 我有一个可切换wifi状态的wifi小部件。 However, if the user changes the wifi state without using my widget, then my widget won't reflect that change. 但是,如果用户在不使用我的窗口小部件的情况下更改了wifi状态,则我的窗口小部件将不会反映该更改。 ex. 例如 wifi is on, user turns the wifi off through the setting, the widget will still show that the wifi is on because it hasn't updated. wifi已打开,用户通过设置关闭了wifi,小部件仍会显示wifi已打开,因为它尚未更新。

How do I get the widget to update after the wifi status has been changed? wifi状态更改后,如何获取小部件进行更新?

Use a BroadcastReceiver to notify such changes 使用BroadcastReceiver通知此类更改

Register a receiver using 使用注册一个接收者

IntentFilter mFilter = new IntentFilter();
mFilter .addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
registerReceiver(yourReceiver, mFilter );

and in the onReceive of yourReceiver class use 并在yourReceiver类的onReceive中使用

if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
//Do something
}

to update your widget 更新您的小部件

You need to register the android.net.conn.CONNECTIVITY_CHANGE and android.net.wifi.WIFI_STATE_CHANGED actions in your receiver. 您需要在接收器中注册android.net.conn.CONNECTIVITY_CHANGE和android.net.wifi.WIFI_STATE_CHANGED操作。

Then in onReceive method, update the widget with the code below: 然后在onReceive方法中,使用以下代码更新窗口小部件:

Intent i = new Intent(context, WidgetProvider.class);
i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WidgetProvider.class)));
context.sendBroadcast(i);

Regards, 问候,

Leonardo Oliveira 莱昂纳多·奥利维拉(Leonardo Oliveira)

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

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