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

查看:86
本文介绍了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. 不能在少于15分钟的间隔内调用JobService的定期更新.
  2. JobService.onStartJob()不可预测的时刻.我们可能会错过确切的分钟来更新分钟数字(第59秒).

以前,我们曾经使用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.

类似这样的方法可以解决问题:

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天全站免登陆