Android O:我们可以做一个时钟小部件吗? [英] Android O: can we make a clock widget?

查看:24
本文介绍了Android O:我们可以做一个时钟小部件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There is a clock widget in our app. The widget needs to be updated every minute to display the time right.

在 Android O 中,建议使用 JobScheduler 进行后台更新.不幸的是,有一些限制.

In Android O, it is advised to use JobScheduler for background updates. Unfortunately, there are limitations.

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

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

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.

您建议如何在 Android O 中实现时钟小部件?

How would you recommend to implement a clock widget in Android O?

现在还可以吗?

推荐答案

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

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.

p>

这样的事情可以解决问题:

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>

这篇关于Android O:我们可以做一个时钟小部件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆