Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update widgets from a service?

Tags:

android

widget

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?

like image 311
Franklin Avatar asked May 15 '10 01:05

Franklin


People also ask

How do you update widgets?

Full update: Call AppWidgetManager. updateAppWidget(int, android. widget. RemoteViews) to fully update the widget.

What is remote view in Android?

android.widget.RemoteViews. A class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.


1 Answers

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.

Note:- 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.

like image 78
CommonsWare Avatar answered Oct 21 '22 20:10

CommonsWare