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?