39

I am trying to implement a simple widget for display on the home screen. The problem I am experiencing is that onUpdate is only being called once when I install the widget. The configuration is below. Note: I will not leave update period at 20 secs as I know that would kill battery (just testing).

Configuration:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
   android:minWidth="294dp"
   android:minHeight="72dp"
   android:updatePeriodMillis="20000"
   android:initialLayout="@layout/my_custom_app_widget">`
</appwidget-provider>`

Manifest Excerpt:

<receiver android:name="MyCustomWidgetProvider">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/my_custom_app_widget_info" />
</receiver>

I am observing the following behavior when I install the widget: In my WidgetProvider class on onReceive is called then onEnabled then onReceive, then onUpdate.

After that the widget displays and onUpdate is never called again. I also inspect the settings of the provder when onUpdate is called and everything set in XML above (e.g. update period) is correct.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
phogel
  • 2,035
  • 3
  • 16
  • 12

3 Answers3

72

While you have android:updatePeriodMillis set to 20 seconds, the minimum actual time is 30 minutes. So, if you have not been waiting 30 minutes to see if there is an update during your testing, you will need to wait a bit longer. Android Documentation - updatePeriodMillis

Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    Thanks. I was going line by line through the docs and they didn't mention that. However, based on your answer I dugfurther and noticed that I am looking at 1.5 docs. The minimum is documented and apparently there is an alarm approach if need higher update frequency. Thanks. – phogel Jan 16 '10 at 16:59
  • 2
    How About the clock or weather update Widgets? How they are updated. – Brijesh Masrani Dec 18 '13 at 17:31
  • 4
    If any of you are building an API: This is not how you do it. If there are restrictions to the value range, make it absolutely clear to the consumer by failing at compile time or worst case runtime. – Nilzor May 13 '15 at 06:27
20

From the SDK:

Note: If the device is asleep when it is time for an update (as defined by updatePeriodMillis), then the device will wake up in order to perform the update. If you don't update more than once per hour, this probably won't cause significant problems for the battery life.

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").

And when SDK 1.6+:

Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes.

Hussain
  • 5,552
  • 4
  • 40
  • 50
duduli
  • 201
  • 2
  • 2
2

as duduli said... use an alarmManager to send an intent to your Widget class, it will receive it in its onReceive method, if you don't know how to do it follow this guide. It worked for me and others

Community
  • 1
  • 1
Saul_programa
  • 341
  • 1
  • 4
  • 13