Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android O: can we make a clock widget?

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.

  1. The periodic updates of JobService can not be called in less than 15 minutes intervals.
  2. The moment JobService.onStartJob() is unpredictable. We may miss the exact moment to update the minute digit (59th second).

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?

enter image description here

like image 922
Pavel Avatar asked Aug 16 '17 06:08

Pavel


People also ask

How do I make my own clock widget?

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.

Can you make your own Android 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.

Is there a clock widget?

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.


1 Answers

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>
like image 179
Gordon Freeman Avatar answered Sep 21 '22 19:09

Gordon Freeman