Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Updating time of clock widget

Posted on November 11, 2022 By

Solution 1 :

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.

Solution 2 :

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.

Problem :

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?

READ  [FIXED] android - Can we generate push notification using onPressed in Flutter (using Firebase)?
Powered by Inline Related Posts

Comments

Comment posted by developer.android.com/guide/topics/appwidgets#CreatingLayout

That is not one of the supported view widgets that you can put in a home screen widget.

Comment posted by Joe

@tyczj well it sure works. tested on multiple devices and launchers.

Comment posted by tyczj

hmm interesting, I will give it a try then

Comment posted by Qtax

@Joe your widget looks great. It’s surprisingly difficult to find decent android stuff without ads, tracking, etc. I would suggest to publish it to F-Droid so that it’s easier to find for people who are looking for open source stuff. A thing I’m missing is for it to open the clock app on tap, and maybe showing next alarm and better handling of denying file access (as that breaks it completely). I will try to make a PR for that when I get the time.

Comment posted by tyczj

There is no onPause/onResume for a widget, tying a widget to an activity does not make sense because then the widget on the home screen will only update if the user opened the app so its not going to show the correct time

Android Tags:android, android-widget

Post navigation

Previous Post: [FIXED] java – Access Binding Adapters in multi module project
Next Post: [FIXED] java – Changing value in on item also changes in another item – RecyclerView Android

Related Posts

[FIXED] Android Google Drive API v2 downloadUrl with access token stopped working Android
[FIXED] java – No field mMaxWidth in class Landroid/widget/ImageView Android
[FIXED] android – Show Text behind recycler view Android
[FIXED] java – How can I detect tab focus changes in an android TabLayout? Android
[FIXED] java – Augment Base Color Class on Android Android
[FIXED] android – CSV example for translation service in Google Play Console Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme