English 中文(简体)
Android: Custom Clock widget Service work-around?
原标题:

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?

最佳回答

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.

问题回答

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.

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.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签