我知道这是可能的,但我无法想出一种从主活动触发我的小部件更新的方法。难道没有可以广播的通用意图吗?
我知道这是可能的,但我无法想出一种从主活动触发我的小部件更新的方法。难道没有可以广播的通用意图吗?
如果你正在使用AppWidgetProvider
,你可以通过以下方式更新:
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 = AppWidgetManager.getInstance(getApplication())
.getAppWidgetIds(new ComponentName(getApplication(), MyAppWidgetProvider.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
当我搜索“如何从服务/操作更新小部件”时,这很有帮助(但可能可以从任何上下文中实现):
Context context = this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_2x1);
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
remoteViews.setTextViewText(R.id.my_text_view, "myText" + System.currentTimeMillis());
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
所以要从活动中更新小部件,您可以这样做:
Intent intent = new Intent(this, DppWidget.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), DppWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);
对我来说有效 :)
试一下这个:
int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), MyWidget.class));
MyWidget myWidget = new MyWidget();
myWidget.onUpdate(this, AppWidgetManager.getInstance(this),ids);
如果你正在使用像ListView、GridView或StackView这样使用集合的小部件来更新小部件的项,请按照以下步骤进行操作:
Context context = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, StackWidgetProvider.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view);
通过使用方法notifyAppWidgetViewDataChanged(),您可以强制小部件项实时获取任何新数据。
int widgetIDs[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), WidgetProvider.class));
for (int id : widgetIDs)
AppWidgetManager.getInstance(getApplication()).notifyAppWidgetViewDataChanged(id, R.id.widget_view);
}
notifyAppWidgetViewDataChanged()
版本。 - Lawrence KestelootPhaethon在Kotlin中的已接受解决方案:
val intent = Intent(this, MyAppWidgetProvider::class.java)
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val ids = AppWidgetManager.getInstance(application).getAppWidgetIds(ComponentName(getApplicationContext(),MyAppWidgetProvider::class.java!!))
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
sendBroadcast(intent)
MyAppWidgetProvider是从AppWidgetProvider派生而来的:
MyAppWidgetProvider : AppWidgetProvider() {
如果您想以编程方式更新小部件,但没有直接访问YourWidgetProvider.class的权限,例如来自其他模块或库,则可以捕获YourWidgetProvider.class.getName()输出的内容并创建一个常量:
val WIDGET_CLASS_NAME = "com.example.yourapplication.YourWidgetProvider"
val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
val widgetManager = AppWidgetManager.getInstance(context)
val ids = widgetManager.getAppWidgetIds(ComponentName(context, WIDGET_CLASS_NAME))
AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(ids, android.R.id.list)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
context.sendBroadcast(intent)
我的待办清单中的例子:
Intent intent = new Intent(this, TodosAppWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), TodosAppWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
sendBrodcast(Intent(this, TodosAppWidget::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val ids = AppWidgetManager.getInstance(application).getAppWidgetIds(ComponentName(application, TodosAppWidget::class.java))
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
})
供参考,我在一个片段中需要这样做:
Application app = getActivity().getApplication();
Intent intent = new Intent(context, RQWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(app).getAppWidgetIds(new ComponentName(app, RQWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
context.sendBroadcast(intent);
widgetId
? - Kris Bintent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
- Mārtiņš Briedisint widgetIDs[] = AppWidgetManager.getInstance(activity).getAppWidgetIds(new ComponentName(activity, widget.class));
这段代码使用了Android中的AppWidgetManager类来获取指定widget类的实例,并获取它们的ID。获取到的ID会存储在一个整型数组widgetIDs中。 - abbasalim