Android 小部件未更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2078122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Widget Not Updating
提问by phogel
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).
我正在尝试实现一个简单的小部件以在主屏幕上显示。我遇到的问题是 onUpdate 在我安装小部件时只被调用一次。配置如下。注意:我不会将更新周期保留在 20 秒,因为我知道这会消耗电池电量(只是测试)。
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.
当我安装小部件时,我观察到以下行为:在我的 WidgetProvider 类中,onReceive 被调用,然后是 onEnabled,然后是 onReceive,然后是 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.
之后小部件显示并且 onUpdate 不再被调用。我还会在调用 onUpdate 时检查提供者的设置,并且上面 XML 中设置的所有内容(例如更新周期)都是正确的。
回答by CommonsWare
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:updatePeriodMillis
设置为 20 秒,但实际最短时间为 30 分钟。因此,如果您在测试期间没有等待 30 分钟以查看是否有更新,则需要等待更长的时间。
回答by duduli
From the SDK:
来自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.
注意:如果设备在更新时间(由 定义updatePeriodMillis
)时处于睡眠状态,则设备将唤醒以执行更新。如果每小时更新不超过一次,这可能不会对电池寿命造成重大问题。
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").
为此,请AppWidgetProvider
使用您收到的 Intent 设置闹钟AlarmManager
。将闹钟类型设置为ELAPSED_REALTIME
或RTC
,只会在设备唤醒时发出闹钟。然后设置updatePeriodMillis
为zero
(“0”)。
And when SDK 1.6+:
当 SDK 1.6+ 时:
Note:Updates requested with updatePeriodMillis
will not be delivered more than once every 30 minutes.
注意:请求的更新updatePeriodMillis
不会每 30 分钟传送一次以上。
回答by Saul_programa
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
正如duduli所说......使用alarmManager向您的Widget类发送意图,它将在其onReceive方法中接收它,如果您不知道如何按照本指南进行操作。它对我和其他人有用