views:

2022

answers:

4

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?

+2  A: 

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.

CaseyB
+3  A: 

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.

Daniel Yankowsky
Thanks for the info, revised the question a bit if you could take a look at that
Anthony Forloney
No worries. These things happen.
Daniel Yankowsky
+1  A: 

Have a look to WorldWideTime. It displays local time widgets for Android. It uses a services to update every minutes.
http://www.lysesoft.com/products/worldwidetime/index.html

lysesoft
+1  A: 

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.

xandy
@xandy, Thank you for the link, it's exactly what I was hoping to find in regards to saving battery life with a `Service`.
Anthony Forloney