如何更新从服务窗口小部件?部件、窗口

2023-09-06 02:08:37 作者:野心ii

我有一个可以被配置为执行某些动作每隔x分钟后台服务。我想实现一个小部件,每一个被我的服务执行该操作时更新。但是,我不能让我的AppWidgetProvider调用任何方式的OnUpdate方法,除了添加窗口小部件到主屏幕的时候。

I have a background service that can be configured to perform some action every x minutes. I want to implement a widget that gets updated every time my service performs that action. However, I can't get the onUpdate method of my AppWidgetProvider called in any way, except when adding a widget to the homescreen.

我试图发送APPWIDGET_UPDATE意图,但OnUpdate中并没有被调用虽然AppWidgetProvider确实收到的意图,因为的onReceive方法被调用。有没有什么方法可以让我从一个服务触发部件更新?

I tried sending APPWIDGET_UPDATE intents but onUpdate did not get called although the AppWidgetProvider did receive the intent since the onReceive method was called. Is there any way I can trigger widget updates from a service?

推荐答案

只要自己一个 RemoteViews 和更新的小窗口直接在服务。你会通过让您的AppWidgetManager:

Just make yourself a RemoteViews and update the widget(s) directly in the Service. You'll get your AppWidgetManager via:

AppWidgetManager mgr=AppWidgetManager.getInstance(this);

一切是正常的。

Everything else is as normal.

如果你真的想迫使现有的 AppWidgetProvider 做的工作,发送广播的自定义操作,以您的组件,并调用的onupdate ()的onReceive自己()当你得到它。

If you really want to force the existing AppWidgetProvider to do the work, send a broadcast with a custom action to your component, and call onUpdate() yourself from onReceive() when you get it.