我们的应用中有一个时钟小部件.小部件需要每分钟更新一次才能正确显示时间.
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.
在 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:我们可以做一个时钟小部件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!