6

I understand that the parameter updatePeriodMillis determines how often an app widget gets updated according to the specification in the widgetproviderinfo.xml present in /res/xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:configure="com.example.appwidget.ConfigurationActivity"
    android:initialLayout="@layout/layout_appwidget_large"
    android:minHeight="115dp"
    android:minWidth="250dp"
    android:updatePeriodMillis="1800000" ><!-- 30mins -->
</appwidget-provider>

This approach has a drawback in that it updates the widget by waking up the phone at the designated interval if the phone is sleeping. So the issue is about battery consumption which is a major issue if the interval is very small.

If, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates based on an alarm that will not wake the device. To do so, set an alarm with an Intent that your AppWidgetProvider receives, using the AlarmManager. Set the alarm type to either ELAPSED_REALTIME or RTC, which will only deliver the alarm when the device is awake. Then set updatePeriodMillis to zero ("0"). to The code would look something like this:

final Intent intent = new Intent(context, UpdateService.class);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 1000*60;
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);

So my question is as follows: Let's say the AlarmManager is used to carry out the update. Additionally, if in the widgetproviderinfo.xml, updatePeriodMillis is NOT set to 0, then Which value will take the precedence? The value specified as part of the Alarm or updatePeriodMillis?

Jonik
  • 80,077
  • 70
  • 264
  • 372
user3575020
  • 137
  • 1
  • 7

2 Answers2

6

Both of them will trigger an onUpdate() call.

If you set an alarm at 20 minutes and your updatePeriodMillis to 30 minutes.

Then u place your widget on the home screen. After 20 minutes, the alarm will trigger an onUpdate to be called (can be any other function of your choosing actually). Then 10 minutes later (in total, 30 mins since widget placed on screen), updatePeriodMillis will trigger an onUpdate(). Then 10 minutes later (40mins since widget placed on screen), the alarm will trigger another onUpdate(). And finally 20 minutes later (60mins since widget placed on screen) updatePeriodMillis will trigger another onUpdate().

Sagar Poshiya
  • 136
  • 3
  • 11
Tikiboy
  • 892
  • 10
  • 20
1

From android DOC: Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes. [enter link description here][1] http://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#updatePeriodMillis

so basically if you set updatePeriodMillis to less than 1800000, android wouldn't call your onUpdate method until 30min passeed

moh.sukhni
  • 2,220
  • 19
  • 24
  • Your explanation is unclear. Do you mean to say that if updatePeriodMillis is present and is configured to less than 1800000, Android will consider 1800000 as a default value?(This would be correct if an Alarm is NOT configured) If updatePeriodMillis < 1800000 and an Alarm is configured, then which would take precedence? Would it depend on which value is larger? Additionally, and more importantly what is the expected behavior when updatePeriodMillis is configured to a value greater than 1800000 and also an Alarm has been configured? Could you please address all the cases? – user3575020 Jun 27 '14 at 11:09