如何更新不同尺寸的widget?
我的应用程序按照此处概述的方案支持多种尺寸的小部件 如何在一个应用程序中添加多个小部件?
它有一个抽象的 BaseWidgetProvider 类处理所有小部件。然后它由 Android 清单中列出的空 WidgetProvider1/2/3/4 类(每种大小一个)进行子类化,一切正常。清单中的注册看起来像这样(典型的 1..4)
<receiver
android:name=".widget.MyWidgetProvider4"
android:label="My Widget size 4" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action
android:name="my.domain.BaseWidgetProvider.UPDATE_WIDGET" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/my_widget_info4" />
</receiver>
我的问题是关于从应用程序发送意图以使用新数据更新小部件。我找不到为所有 4 个提供者发送单个意图的方法,并且目前每次发送 4 个单独的意图,
for (Class cls : BaseWidgetProvider.WIDGET_PROVIDER_CLASSES) {
Intent intent = new Intent(mApp.context(), cls);
intent.setAction(BaseWidgetProvider.UPDATE_WIDGET);
this.sendBroadcast(intent);
}
其中
public static final Class[] WIDGET_PROVIDER_CLASSES = new Class[] {
WidgetProvider4.class,
WidgetProvider1.class,
WidgetProvider2.class,
WidgetProvider3.class
};
这是向所有小部件尺寸发送更新意图的最佳方式吗(请注意,通常用户只有零个或一个小部件)?
是否有合理的方法来仅发送单个意图?
有没有办法让单个接收器可以更新所有四种尺寸的小部件实例?
请注意,基本提供程序类不关心小部件的大小,并且对所有大小都执行完全相同的操作。唯一的区别是用户屏幕上的初始小部件大小。如果用户调整小部件的大小(例如使用 Launcher Pro),它就可以正常工作。
My app has supports multiple size widgets per the scheme outlined here how to add multiple widgets in one app?
It has a single abstract BaseWidgetProvider class that handle all the widgets. It is then subclassed by empty WidgetProvider1/2/3/4 classes (one per size) that are listed in the Android Manifest and everything works just fine. The registration in the manifest looks like this (typical for 1..4)
<receiver
android:name=".widget.MyWidgetProvider4"
android:label="My Widget size 4" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action
android:name="my.domain.BaseWidgetProvider.UPDATE_WIDGET" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/my_widget_info4" />
</receiver>
My question is about sending an intent from the app to update the widgets with new data. I could not find a way to send a single intent for all 4 providers and am currently sending 4 seperate intents each time
for (Class cls : BaseWidgetProvider.WIDGET_PROVIDER_CLASSES) {
Intent intent = new Intent(mApp.context(), cls);
intent.setAction(BaseWidgetProvider.UPDATE_WIDGET);
this.sendBroadcast(intent);
}
where
public static final Class[] WIDGET_PROVIDER_CLASSES = new Class[] {
WidgetProvider4.class,
WidgetProvider1.class,
WidgetProvider2.class,
WidgetProvider3.class
};
Is this the best way to send an update intent to all widget sizes (note that typically the user has only zero or one widgets)?
Is there a reasonable way to send only a single intent?
Is there a way to have a single receiver that will update widget instances of all four sizes?
Note that the base provider class does not care about the widget size and does exactly the same for all sizes. The only difference is the initial widget size on the user's screen. If the user resizes the widget (e.g. using Launcher Pro), it works just fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将具有一个意图操作的意图过滤器添加到 Manifest 文件中所有小部件提供程序的声明中,例如:
然后只需从应用程序中以广播形式发送此意图。
在这种情况下,意图将由所有小部件提供商处理。
Add an intent filter with one intent action into the declarations of all of your widget providers in Manifest file, e.g.:
And then just send this intent from your application as broadcast.
In this case the intent will be handled by all your widget providers.