views:

197

answers:

2

I have a background service that can be configured to perform some action every x minutes. I want to implement a widget that gets updated every time my service performs that action. However, I can't get the onUpdate method of my AppWidgetProvider called in any way, except when adding a widget to the homescreen.

I tried sending APPWIDGET_UPDATE intents but onUpdate did not get called although the AppWidgetProvider did receive the intent since the onReceive method was called. Is there any way I can trigger widget updates from a service?

+1  A: 

Just make yourself a RemoteViews and update the widget(s) directly in the Service. You'll get your AppWidgetManager via:

AppWidgetManager mgr=AppWidgetManager.getInstance(this);

Everything else is as normal.

If you really want to force the existing AppWidgetProvider to do the work, send a broadcast with a custom action to your component, and call onUpdate() yourself from onReceive() when you get it.

CommonsWare
Thanks! The second suggestion is what I did and it works like a charm. Why didn't I think of that myself ... :)
Franklin
onUpdate needs an array int[] appWidgetIds as argument. How do you calculate it when you are in the onReceive method ?
Guido
@Guido: Call `getAppWidgetIds()` on `AppWidgetManager`: http://developer.android.com/reference/android/appwidget/AppWidgetManager.html
CommonsWare
+1  A: 

CommonsWare or Franklin - can you give sample source on how you do the second approach ?

TIA

Shimon Shnitzer
Are you still looking for a sample?
Franklin
I also need a exmple to find a ComponentName which the getAppWidgetIds() requires.
shiami