Jake,

There are two scenarios involved:

1. Android calls the widget provider's onUpdate() - when the widget is first shown and then regularly at updatePeriodMillis intervals, if specified. Note that automatic updates based on updatePeriodMillis are limited to once every 30 minutes since Android 2.x.

2. You are free to update your widgets any time, by doing something like this:

ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);

RemoteViews updateViews = ...... specify new widget state here ......

manager.updateAppWidget(thisWidget, updateViews);

For your calendar example, obviously the second scenario has to be involved.

As for performing updates when necessary (and only then), your code needs to contain some logic to figure out the appropriate time for the next update (based on actual calendar events and how much in advance they are supposed to be displayed). Then set an alarm using AlarmManager to update the widget.

Note that AlarmManager does not have to be used in a service, although sample code typically does. An alarm simply fires off a PendingIntent. This intent can be specific to your code, triggering a widget update just in time to for next scheduled calendar event.

A good place to receive this special intent is in your AppWidgetProvider - since it's a BroadcastReceiver anyway.

Hope this helps.

-- Kostya

24.06.2010 18:31, Jake Colman пишет:
I have some questions about the correct approach for updating an
AppWidget's display.

Since this is a beginners forum, please let me state what I already do
know:

1) I can use android::updatePeriodMillis to specify the update
    interval.  When this interval has elapsed the phone is woken up.
    Intervals of less than one hour are not a good idea.

2) Updates can be triggered by an alarm that will not actually wake up
    the phone.

What I'm confused about is how/why/when to trigger an update.

Let's say I am creating an AppWidget that will display the time of my
next appointment.  The widget needs to know the current time and the
time of my next appointment.  When the current time is after the current
appointment the widget has to display the time of the next appointment.

For this widget to work it must be regularly checking the time and the
calendar since at any minute it might be time to display the next
appointment.  Does this mean I have to update the widget's display every
minute?  Clearly not, but how else?  In actuality, I only really need to
update the display if the user is looking at the screen.  But do we only
update if the screen is active?  If so, what about if that update is a
time-consuming process?  The UI would then appear non-responsive.

So what the is correct approach for this kind of problem?

Thanks!



--
Kostya Vasilev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to