Android:自定义时钟小部件服务解决方法?

发布于 2024-08-15 09:03:38 字数 510 浏览 4 评论 0原文

我对为主屏幕开发时钟小部件感兴趣,并在阅读 主屏幕小部件教程,我想知道是否有一个预先存在的服务我可以参考来更新当前时间而不是重新发明轮子?

我在 Android 手机上下载了复古时钟应用程序,并注意到当我单击它时,它会弹出闹钟设置,但使用默认的 Google 模拟时钟小部件,单击不执行任何操作。

这是因为 Retro Clock 小部件实现了闹钟服务吗?如果是这样,我该如何引用该服务?还是我这一切都错了并且被误解了?

任何帮助表示赞赏。

编辑:

我相信实施更新时钟的服务会极大地消耗电池寿命,有什么解决办法的想法或有助于阐明使用 Service 的任何性能问题吗?

I was interested in developing a clock widget for the homescreen and upon reading Home Screen Widgets tutorial, I wondered if there is a pre-existing Service I could reference for updating the current time rather than re-inventing the wheel?

I download the Retro Clock application on my android phone and noticed that when I click it, it pops up the Alarm Clock settings, but with the default Google Analog Clock widget, upon click does nothing.

Is that because the Retro Clock widget implements the Alarm Clock service? If so, how can I go about referencing that service? Or do I have this all wrong and misunderstood?

Any help is appreciated.

EDIT:

I believe implementing the service to update the clock would drain the battery life tremendously, any ideas on a work around or help shed some light on any performance issues with using Service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2024-08-22 09:03:38

我的猜测:复古时钟小部件响应触摸,并触发启动闹钟设置页面的意图。您可以查看 Android 源代码以了解其意图。要了解如何连接您的小部件以实际触发意图,您可以查看 Android 开发指南中的代码示例


对于时钟小部件,您似乎有两个要求。它需要至少每分钟更新一次,但不需要在设备睡眠时更新。这是我找到的一句话:

注意:如果设备在更新时处于睡眠状态(由 updatePeriodMillis 定义),则设备将被唤醒以执行更新。如果每小时更新次数不超过一次,这可能不会对电池寿命造成重大问题。 但是,如果您需要更频繁地更新和/或不需要在设备睡眠时更新,那么您可以根据不会唤醒设备的闹钟来执行更新。为此,请使用 AlarmManager 使用 AppWidgetProvider 接收的 Intent 设置警报。将闹钟类型设置为 ELAPSED_REALTIME 或 RTC,这样只会在设备唤醒时发出闹钟。然后将 updatePeriodMillis 设置为零(“0”)。

(来自应用程序小部件指南


顺便说一句,我得到了一个有机会与 HTC Hero 一起玩。我注意到,当您将英雄从睡眠中唤醒时,其模拟时钟小部件上的指针会从 12 点开始。然后,它会旋转指针以达到正确的时间。这可能是因为他们在手机休眠时不会更新。

My guess: the Retro Clock widget responds to touches, and fires off the intent that launches the alarm clock setting page. You could look in the Android source code to figure out what intent that would be. To see how to wire up your widget to actually fire the intent, you can look at the code example in the Android dev guide.


For a clock widget, it seems like you have two requirements. It needs to update at least once per minute, but it doesn't need to update while the device is asleep. Here's a quote I found:

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

(from the App Widgets guide)


Incidentally, I got a chance to play with an HTC Hero. I noticed that, when you wake a Hero from sleep, the hands on its analog clock widget start at 12. Then, it spins the hands around to get to the proper time. This is probably because they don't update it while the phone is sleeping.

泪冰清 2024-08-22 09:03:38

看看这个。它通过服务实现时钟,并订阅屏幕关闭事件,以便在屏幕关闭时暂停服务以节省电池。

Check this out. It implements the clock with a service, and it subscribe to the screen-off event so that it will suspend the service while screen is off to save some battery.

混浊又暗下来 2024-08-22 09:03:38

它弹出闹钟的原因是因为它实现了 OnClick 方法,并从那里启动闹钟。您可以从 OnClick 启动任何活动,在很多情况下,小部件使用它来启动自己的配置实用程序。

The reason it pops up the alarm clock is because it has implemented the OnClick method and from there it launches the alarm clock. You can launch any activity from the OnClick, in a lot of cases widgets use that to launch their own configuration utility.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文