Android O:我们可以做一个时钟部件吗?
Android O: can we make a clock widget?
我们的应用程序中有一个时钟小部件。
小部件需要每分钟更新一次才能正确显示时间。
在AndroidO中建议使用JobScheduler进行后台更新。
不幸的是,有限制。
- JobService 的定期更新不能在小于 15 分钟的间隔内被调用。
- 时刻 JobService.onStartJob() 是不可预测的。我们可能会错过更新分钟数字的确切时刻(第 59 秒)。
在 O 之前,我们使用 运行 后台服务 Handler.postDelayed() 来更新小部件中的时间。
O中后台服务可以被系统终止
您建议如何在 Android O 中实现时钟小部件?
现在可以吗?
这是一个小部件;它不需要一直更新——只有当它可见时才更新。当您的小部件不是 运行 时,为什么要浪费电池来跟踪当前时间?
当小部件可见时,查找当前时间并立即绘制。此外,计算下一分钟的顶部时间,以及 post 延迟消息或 Runnable 到将在该时刻触发的处理程序。当 message/Runnable 执行时,更新您的时间显示并重新 post 下一分钟开始的 message/Runnable。
您还应该为警报管理器在当前时间更改时发出的 Intent.ACTION_TIME_CHANGED
广播注册一个接收器;您需要重新计算您的显示时间以及下一次更新将在何时响应该广播。
使用"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>
我们的应用程序中有一个时钟小部件。 小部件需要每分钟更新一次才能正确显示时间。
在AndroidO中建议使用JobScheduler进行后台更新。 不幸的是,有限制。
- JobService 的定期更新不能在小于 15 分钟的间隔内被调用。
- 时刻 JobService.onStartJob() 是不可预测的。我们可能会错过更新分钟数字的确切时刻(第 59 秒)。
在 O 之前,我们使用 运行 后台服务 Handler.postDelayed() 来更新小部件中的时间。 O中后台服务可以被系统终止
您建议如何在 Android O 中实现时钟小部件?
现在可以吗?
这是一个小部件;它不需要一直更新——只有当它可见时才更新。当您的小部件不是 运行 时,为什么要浪费电池来跟踪当前时间?
当小部件可见时,查找当前时间并立即绘制。此外,计算下一分钟的顶部时间,以及 post 延迟消息或 Runnable 到将在该时刻触发的处理程序。当 message/Runnable 执行时,更新您的时间显示并重新 post 下一分钟开始的 message/Runnable。
您还应该为警报管理器在当前时间更改时发出的 Intent.ACTION_TIME_CHANGED
广播注册一个接收器;您需要重新计算您的显示时间以及下一次更新将在何时响应该广播。
使用"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>