简体   繁体   English

当Wifi状态更改时会自动更新的小部件

[英]Widget that updates itself when Wifi state changes

I'm currently working on a widget that should update itself whenever the WiFi state changes (Connected or Disconnected). 我目前正在使用一个小部件,只要WiFi状态发生变化(已连接或已断开),该小部件都应会自动更新。

I've got a widget running, and i've created a custom BroadcastReceiver to receive a broadcast whenever the Wifi state changes, and this is all working fine, but i've run into a problem. 我有一个小部件正在运行,并且我创建了一个自定义的BroadcastReceiver以便在Wifi状态更改时接收广播,并且一切正常,但是我遇到了问题。

The BroadcastReceiver sometimes simply shuts down, so it doesn't receive the broadcasts when needed. BroadcastReceiver有时只是关闭,因此在需要时它不会接收广播。 I've read on http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html that ' After the onReceive() of the receiver class has finished, the Android system is allowed to recycle the receiver. 我已经在http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html上阅读了以下内容 ' 在接收器类的onReceive()完成后,允许Android系统回收接收器。 ' '

So that's my best guess at what's going on.. But now i want to know how to stop that? 因此,这是我对发生的事情的最佳猜测。但是,现在我想知道如何阻止这种情况?
Basically i want to have my BroadcastReceiver to ALWAYS be alive and listening for broadcasts while the widget is placed on the screen. 基本上,我希望我的BroadcastReceiver始终处于活动状态,并在将小部件放在屏幕上的同时收听广播。

Is that doable with a BroadcastReceiver in a widget? 这对小部件中的BroadcastReceiver可行吗? Or should i try to use a Service that holds the BroadcastReceiver instead? 还是应该尝试使用包含BroadcastReceiver的服务?

Hopefully someone can help me out with this problem. 希望有人可以帮助我解决这个问题。

If you need get when wifi is connected/disconnected only for the widget, you can try add in the manifest of the widget the action for wifi connection changes like you add in the BroadcastReceiver in the manifest, because the widget class AppWidgetProvider extends BroadcastReceiver 如果仅在小部件连接/断开wifi时需要获取信息,则可以尝试在小部件清单中添加wifi连接更改操作,就像在清单中的BroadcastReceiver中添加一样,因为小部件类AppWidgetProvider扩展了BroadcastReceiver

<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE"/>
</intent-filter>

And in the widget use the onReceive method 然后在小部件中使用onReceive方法

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
        // save the connected state to get in onUpdate
        this.connected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
        // update all widgets
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int appWidgetIds[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, MyWidget.class));
        onUpdate(context, appWidgetManager, appWidgetIds);
    } else {
        super.onReceive(context, intent);
    }
}

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

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