There is a clock widget in our app. The widget needs to be updated every minute to display the time right.
In Android O, it is advised to use JobScheduler for background updates. Unfortunately, there are limitations.
Prior O we used to run a background service with Handler.postDelayed() to update the time in the widget. In O the background service can be terminated by the system.
How would you recommend to implement a clock widget in Android O?
Is this even possible now?
Procedure: Download Make Your Clock widget from the link given below and install it on your Android device. Tap and hold on the Home Screen, go to Widgets and scroll down to Make your Clock Widget. Select from the list of 8 widgets depending on your need of rows and columns required for the widget.
You can create your own widget with the help of third-party apps. One such app is KWGT Kustom Widget Maker, and for the purpose of this tutorial, we are using it to design a simple event reminder widget that links to the Google Calendar.
Digital clock widget is a home screen digital time and date widget for Android. It looks just like the one on the iPhone unlock screen.
Use "TextClock" widget control, it is supported by RemoteView functionality and it updates every minute by it self, no need to use those fancy JobScheduler jobs.
Something like this will do the trick:
<LinearLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextClock
android:id="@+id/dateText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:format12Hour="@string/date_text_format_12h"
android:format24Hour="@string/date_text_format_24h"
android:gravity="bottom|center_horizontal"
android:textColor="@android:color/black"
android:textSize="@dimen/date_text_size_default"/>
<TextClock
android:id="@+id/timeText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:format12Hour="@string/time_text_format_12h"
android:format24Hour="@string/time_text_format_24h"
android:gravity="top|center_horizontal"
android:textColor="@android:color/black"
android:textSize="@dimen/time_text_size_default"
android:textStyle="bold"/>
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With