Jake,

With each widget having its own settings, you are pretty much forced to update them individually.

I would have the alarm receiver obtain all widget Ids and send them using startService all at once. The service would then do the filtering for stale Ids, combined with loading actual settings.

My code looks like this :

int[] widgetIdList = ...
WidgetPrefs prefs = ....
for (widgetId : widgetIdList) {
    if (prefs.load(widgetId)) { build and push an update }
}

If prefs.load returns false, it means this the widgetId is stale.

To your specific questions:

1 - Calling startService once for the entire list should be more efficient;

2 - AppWidgetManager: void updateAppWidget(int appWidgetId, RemoteViews views)

3 - Each RemoteViews update needs to be self-sufficient, that is, contain all the data items (setTextViewText, etc.) as well as PendingIntent's.

Do not push separate, incremental, RemoteViews: one for text views, then another for pending intents, etc. If you do, your widget will stop working after a home screen orientation change or if the Launcher is kicked out of memory.

http://kmansoft.wordpress.com/2010/05/23/widgets-and-orientation-changes/

The lifetime of widget receiver does not matter here. The contents of RemoteViews is saved by the launcher, and PenidingIntent's are saved by "the system".

Finally, do not ignore onUpdate. The launcher may have its reasons to rebuild the widgets, such as when the user changes the phone's UI language, and maybe in other cases as well.

-- Kostya

04.05.2011 1:56, Jake Colman пишет:
Kostya,

Hmmmmmm.

Whenever the alarm in AppWidgetProvider is triggered, I have to update
all widget instances as per the widget-specific preference.  Based on
what you have suggested, does the following make sense:

1) In the AppWidgetProvider's onReceive() method when it receives the
    intent from the alarm, it cycles through all valid widgetids and
    starts the service with an intent containing that widgetid.
    Actually, I can just invoke with service once with an intent
    containing the list of all valid widgetids.

2) The service cycles through the list of widgetids and updates each
    widget's remoteview using the widget-specific preference.  I didn't
    check it just now but I'm sure I can get each widget's RemoteView and
    update each one separately.

3) I currently set the remoteview's onClickPendingIntent in the
    service.  Which means I am setting it up every time the service
    updates the remoteview.  Is that really the best way to do it?  Can I
    set the remoteview's onClickPendingIntent in the AppWidgetProvider
    or, since the AppWidgetProvider regularly gets destroyed I cannot do
    it there?

...Jake

Kostya Vasilyev -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to