I’m trying to update a widget from a service… the service start a thread that execute this code
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(Worker.this);
RemoteViews remoteViews = new RemoteViews(Worker.this.getPackageName(), R.layout.mywidget);
ComponentName projectWidget = new ComponentName(Worker.this, MyWidget.class);
remoteViews.setTextViewText(R.id.widgetstate, "update");
appWidgetManager.updateAppWidget(projectWidget, remoteViews);
the onUpdate method just print a log, but this log is printed only when I create the widget, not everytime the thread cycles… where I’m wrong?
Thanks in advance
You’re not wrong. You just update the widget without calling
onUpdate(). It’s just a convinience method that gets called in response to theACTION_APPWIDGET_UPDATEbroadcast by the system. You don’t have to call it to update a widget, it also just works like you did here. ¹To invoke it you can send the broadcast or call it by hand though, it’s a good idea to keep the widget update functionality in one place from a code structure point of view to find things easily.
¹ Take a look at the
AppWidgetProvidersource, line 56. The wholeAppWidgetProvideris just aBroadcastReceiverthat does some work for you already – and it does start the update in the same way.