tags:

views:

25

answers:

1

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);
+2  A: 

Yes, it's possible. You'll find the action in AppWidgetManager:

intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE)

Edit: You will need to provide the ids of the widgets you want to update. Below is a complete sample.

AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
ComponentName widgetComponent = new ComponentName(context, YourWidget.class);
int[] widgetIds = appWidgetManager.getAppWidgetIds(widgetComponent);
Intent update = new Intent();
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
context.sendBroadcast(update);
Justin
Thanks. I see the intent fire in adb logcat, but the widget doesn't update. Anything else I should be doing? I do have APPWIDGET_UPDATE as an intent filter in my manifest file already.
NPike
`onUpdate()` isn't called? I'll have to get back to you on that.
Justin
You will need to provide ids of widgets to update. If you would like to update all instances of your widget, you may obtain a complete list of ids from the `AppWidgetManager` (I've edited my comment to include the code).
Justin