My app has a widget that shows today's date and need to be updated on midnight. The widget is defined in the manifest as
<manifest>
...
<application>
...
<receiver
android:name=".MyWidgetProviderClass"
android:label="MyLabel" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_icon_info" />
</receiver>
</application>
</manifest>
And the widget info file is
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/my_layout"
android:minHeight="40dp"
android:minWidth="294dp"
android:updatePeriodMillis="3600000" >
</appwidget-provider>
This causes the widget to get updated anytime between midnight and 1pm but I want to to get updated closer to midnight so I added to the app an intent receiver
<receiver
android:name=".MyWidgetUpdaterClass"
android:enabled="true" >
<intent-filter>
<action android:name="MyAction" />
</intent-filter>
</receiver>
and a static method
public static void setMidngintAlarms(Context context) {
Intent intent = new Intent("MyAction");
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, intent, 0);
long interval = 24*60*60*1000;
long firstTime = SystemClock.elapsedRealtime() +
millisecondsToNextMidnight();
AlarmManager am = (AlarmManager)context.getSystemService(
Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, interval, pendingIntent);
}
(since alarm manager requests with the same intent overwriting each other there is not harm in calling setMidngintAlarms multiple times).
I tried calling setMidnightAlarms() from few places (from the app's main activity, from the AppWidgetProvider's onUpdate(), etc). Everything works well and the alarm manager intents are received and the widget is updated but only as long as the app runs. If I killed the app, the widgets stops updating on midnight.
Any idea how to cause the widgets to update on midnight? Do I need to add a service? If so how? Any example?
I am a little bit surprised how non trivial it is to get this basic functionality to work.