Is it possible to throw an intent for APPWIDGET_UPDATE programmatically?

Would like a button in my widget to fire the APPWIDGET_UPDATE intent on the widget class to force an update, but I dont see APPWIDGET_UPDATE as a static field in Intent.

Is this possible, and how would one do this?

Intent intent = new Intent(context, BaseWidgetProvider.class);
intent.setAction({APPWIDGET_UPDATE INTENT HERE})
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

views.setOnClickPendingIntent(R.id.MyWidgetButton, pendingIntent);
9
задан NPike 25 August 2010 в 20:59
поделиться

1 ответ

Да, возможно. Вы найдете действие в AppWidgetManager:

intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE)

Изменить: вам нужно будет указать идентификаторы виджетов, которые вы хотите обновить. Ниже приведен полный образец.

AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
ComponentName widgetComponent = new ComponentName(context, YourWidget.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widgetComponent);
Intent update = new Intent();
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
context.sendBroadcast(update);
15
ответ дан 4 December 2019 в 12:15
поделиться
Другие вопросы по тегам:

Похожие вопросы: