Android O:我们可以制作时钟部件吗?

Pav*_*vel 10 android widget android-jobscheduler

我们的应用程序中有一个时钟小部件.小部件需要每分钟更新一次以显示正确的时间.

在Android O中,建议使用JobScheduler进行后台更新.不幸的是,存在局限性.

  1. 不能在15分钟的间隔内调用JobService的定期更新.
  2. JobService.onStartJob()的时刻是不可预测的.我们可能会错过更新分钟数字(第59秒)的确切时刻.

在O之前,我们曾经使用Handler.postDelayed()运行后台服务来更新小部件中的时间.在O中,后台服务可以由系统终止.

您如何推荐在Android O中实现时钟小部件?

这现在有可能吗?

在此输入图像描述

Gor*_*man 7

使用"TextClock"小部件控件,它受RemoteView功能支持,它每分钟都自动更新,无需使用那些花哨的JobScheduler作业.

像这样的东西可以解决这个问题:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案不适用于模拟时钟.有谁知道是否可以在Android Oreo中创建模拟时钟? (4认同)