以编程方式更改 android:updatePeriodMillis

Changing android:updatePeriodMillis programmatically

好的,所以我做了一些研究,似乎一致认为你不能以编程方式更新android:updatePeriodMillis

看来你必须改用 AlarmManager,这就像用大锤敲坚果一样......奇怪的是 API 不只是让你更新核心 updatePeriodMillis.

依赖网络上的内容总是存在轻微的风险,因为 API 往往会发展,旧的答案不再相关。

所以我只是在检查情况是否仍然如此。例如http://developer.android.com/guide/topics/appwidgets/index.html的指南至少提示可以改变AppWidgetProvider的更新周期。在讨论 updatePeriodMillis 时,它说:

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

然后然后继续谈论使用AlarmManager,但显然只是为了避免唤醒设备而不是改变更新周期。

感谢您对此的任何帮助。

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

是的,还是无法更新updatePeriodMillis。这太糟糕了,因为我很想看到 updateUpdatePeriodMillis() 方法。特别是如果这是通过某种构建器或事务对象完成的,这意味着它也可以通过 updateUpdateUpdatePeriodMillis() 方法进行更改。

:-)

It seems that you have to use AlarmManager instead

您也可以在 Android 5.0+ 上使用 JobScheduler,但我怀疑您也不会喜欢那么多。

您还可以允许用户配置 updatePeriodMillis 的倍数,然后仅在每 N 次更新后执行您的工作,尽管这不是非常有效。

或者,您根本无法定期更新您的应用程序小部件,而是仅根据应用程序功能而不是根据时间在需要时更新它。恕我直言,这是大多数应用程序应该做的。