以编程方式更新的活动/服务/接收器插件接收器、插件、方式

2023-09-12 01:30:40 作者:清风

我知道这是可能的,但我不能想出一个办法,从主要活动触发我的窗口小部件的更新。是不是有一些通用的意向,我可以播出?

I know it's possible, but I can't figure out a way to trigger an update of my widget from the main activity. Isn't there some general intent I can broadcast?

推荐答案

如果您的是的使用AppWidgetProvider,你可以更新它是这样的:

If you are using an AppWidgetProvider, you can update it this way:

Intent intent = new Intent(this,MyAppWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int[] ids = {widgetId};
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);