【Android】桌面Widget动态刷新

本文探讨了Android桌面Widget的动态刷新,强调了官方建议的更新频率和电池寿命的考虑。当`updatePeriodMillis`设置不当,可能在1.6及以上SDK版本中失效。作者尝试了Handler、Service和AlarmManager等不同更新方式,并遇到各种问题,如应用升级后的异常、线程挂掉等。最终,作者推荐在`onUpdate`中设置Alarm,并在Alarm触发时仅调用`onReceive`方法,避免调用`onUpdate`,以解决Widget异常刷新的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于桌面widget的动态刷新有这样一个配置,

在res/xml下<appwiget-provider>下有一个属性

android:updatePeriodMillis="86400000"

官方的注释是:

The updatePeriodMillis attribute defines how often the App Widget framework should request an update from the AppWidgetProvider by calling the onUpdate() callback method. The actual update is not guaranteed to occur exactly on time with this value and we suggest updating as infrequently as possible—perhaps no more than once an hour to conserve the battery. 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.

中文翻译:

updatePeriodMillis这个属性规定了AppWiget框架应该多久发送一次请求通过调用AppwidgetProvider的onUpdate()方法来更新,实际的更新并不能保证根据这个值准时的更新,我们建议尽可能频率少的进行更新操作,最好不要超过1个小时更新一次,为了保存电池的电量。你页可以给出配置允许用户来调整更新的频率,就像一些人希望股票行情追踪器每15分钟更新一次,或者仅仅一天更新4次

另外还给出了一个备注:

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 b
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值