Reputation: 73826
I am looking at creating a clock widget that tell the current time but if the minimum time you can set for the widget is 15 minutes thats obviously not going to work.
I also see suggestions on using AlarmManager
but then I worry about the battery life.
I also see the intent action Intent.ACTION_TIME_TICK
that fires every minute and seems like what I want but as the documentation says
You cannot receive this through components declared in manifests, only by explicitly registering
So that seems like its not going to work for a widget
So the question is, is there a "correct" way to build a clock widget that does not kill the battery and still updates the current time?
Upvotes: 3
Views: 2349
Reputation: 2243
If you are talking about a Digital clock , Android added a TextClock layout widget on API level 17 . you can simply add it to your widget XML and it will automatically show the time. Example:
<android.widget.TextClock
android:id="@+id/clock"
android:format24Hour="k:mm"
android:format12Hour="k:mm"
android:gravity="center" />
I actually just made (this week) an open source android digital clock widget , feel free to browse and use my code. pull requests will be much appreciated! I'm sure it will be helpful to you since I stumbled a bunch of surprising walls on the way. https://github.com/y0av/MaterialClockWidget
if you want anything other then a digital clock you will need to use an AlarmManager like you mentioned.
Upvotes: 4
Reputation: 467
Make it tick only when the user is watching. So in onResume make it to current time and update it every minute or something, then in onPause stop updating it and kill the timer,handle,rxInterval or whatever you are using to update the view periodically.
Upvotes: 0