2

OK, so I have done some research and the consensus seems to be that you can't update android:updatePeriodMillis programatically.

It seems that you have to use AlarmManager instead, which seems like using a sledghammer to crack a nut... odd that the API doesn't just let you update the core updatePeriodMillis.

There's always a slight risk with relying on what's out there on the web, since APIs tend to develop and old answers are no longer relevant.

So I'm just checking that this is still the case. For example, the guide at http://developer.android.com/guide/topics/appwidgets/index.html at least hints that it is possible to change the update period of the AppWidgetProvider. When discussing updatePeriodMillis it says:

"You might also allow the user to adjust the frequency in a configuration—some people might want a stock ticker to update every 15 minutes, or maybe only four times a day."

And then goes on to talk about using AlarmManager but apparently only in relation to avoiding waking the device rather than to changing the update period.

Thanks for any help on this.

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181

1 Answers1

3

So I'm just checking that this is still the case.

Yes, it is still the case that you cannot update updatePeriodMillis. Which is too bad, as I'd love to see an updateUpdatePeriodMillis() method. Particularly if this were done via some sort of builder or transaction object, implying that it too might be changed via an updateUpdateUpdatePeriodMillis() method.

:-)

It seems that you have to use AlarmManager instead

You could use JobScheduler on Android 5.0+ as well, though I suspect that you won't like that much either.

You could also allow the user to configure some multiple of updatePeriodMillis, then only do your work every N updates, though this isn't terribly efficient.

Or, you could not update your app widget periodically at all, instead updating it only as needed based on app functionality, rather than based on time. IMHO, this is what most apps should be doing.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Hmm, OK thanks. It's odd that Google acknowledge in their own guide that the developer might well contemplate allowing the user to update the interval, and yet don't provide a method to do this! You have to abandon the core method and adopt a completely different solution, or apply an ugly kludge. Shouldn't be so difficult to do something so simple. Only doing some work every N updates seems like a reasonable way to go. Will look into AlarmManager anyway. – drmrbrewer Jan 21 '15 at 18:53
  • I'm starting to understand, why setting small value for updatePeriodMillis in the manifest and then skipping some updates is, at leastin some cases, much worse than using Alarm Manager or Job Sheduling... In the first case your widget will be redrawn on each update, e.g. list with data will be requeried... In the approach with an independent alarm/job you may actually do nothing and spend close to zero resources if not needed! – yvolk Nov 01 '19 at 06:07