我有兴趣为主屏幕开发一个时钟小部件,并在阅读后 Home Screen Widgets tutorial , 我想知道是否有一个预先存在的 Service
我可以引用更新当前时间而不是重新发明轮子?
我在我的 Android 手机上下载了 Retro Clock 应用程序并注意到当我点击它时,它会弹出闹钟设置,但使用默认的 Google 模拟时钟小部件,点击后什么都不做。
是不是因为 Retro Clock 小部件实现了闹钟服务?如果是这样,我该如何引用该服务?还是我完全错误和误解了这一切?
感谢任何帮助。
编辑:
我相信实现更新时钟的服务会极大地耗尽电池生命周期,关于解决方法的任何想法或有助于阐明使用 Service
的任何性能问题?
最佳答案
我的猜测:Retro Clock 小部件响应触摸,并触发启动闹钟设置页面的 Intent 。您可以查看 Android 源代码以弄清楚那是什么 Intent 。要查看如何连接小部件以实际触发 Intent ,您可以查看 code example in the Android dev guide .
对于时钟小部件,您似乎有两个要求。它需要每分钟至少更新一次,但在设备休眠时不需要更新。这是我找到的一句话:
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 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").
顺便说一下,我有机会和 HTC Hero 一起玩。我注意到,当你将 Hero 从 sleep 中唤醒时,其模拟时钟小部件上的指针从 12 点开始。然后,它会旋转指针以到达正确的时间。这可能是因为他们在手机休眠时不更新它。
关于Android:自定义时钟小部件服务解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1942101/