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