[英]Why does my Pebble watchface not update the time every minute?
I'm learning watchface development. 我正在学习表面开发。 I have been following the Pebble guide closely, so 80% of my code is the same as their sample code.
我一直在密切关注Pebble指南,因此80%的代码与示例代码相同。 I'm probably missing something very small, but my face does not seem to be correctly subscribed to the time service.
我可能错过了很小的东西,但我的脸似乎没有正确订阅时间服务。
What am I doing wrong? 我究竟做错了什么?
In init()
, I have: 在
init()
,我有:
tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
tick_timer_service_subscribe(DAY_UNIT, tick_handler);
Here's tick_handler
: 这是
tick_handler
:
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_time();
}
Here's update_time
: 这是
update_time
:
static void update_time() {
time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
static char time_buffer[] = "00:00";
static char date_buffer[] = "00/00/00";
if (clock_is_24h_style() == true) {
strftime(time_buffer, sizeof(time_buffer), "%H:%M", tick_time);
} else {
strftime(time_buffer, sizeof(time_buffer), "%I:%M", tick_time);
}
text_layer_set_text(s_time_layer, time_buffer);
strftime(date_buffer, sizeof(date_buffer), "%D", tick_time);
text_layer_set_text(s_date_layer, date_buffer);
}
The face only updates the time when it first loads (by calling update_time
). face仅更新首次加载的时间(通过调用
update_time
)。
TimeUnits
is a bit mask. TimeUnits
有点掩盖。 You set a mask and then call tick_timer_service_subscribe
once. 您设置了一个掩码,然后调用
tick_timer_service_subscribe
一次。 Your second call using DAY_UNITS is changing your subscription. 您使用DAY_UNITS进行的第二次通话正在改变您的订阅。 To subscribe to both units, you bitwise-or your mask bits:
要订阅这两个单元,您按位或您的掩码位:
tick_timer_service_subscribe(MINUTE_UNIT | DAY_UNIT, tick_handler);
Notice how your tick handler has a TimeUnits argument. 请注意您的tick处理程序如何具有TimeUnits参数。 That argument tells you which unit triggered the handler.
该参数告诉您哪个单元触发了处理程序。 In your case, you always want to update the time and it appears DAY_UNIT is redundant.
在您的情况下,您总是希望更新时间,看起来DAY_UNIT是多余的。 But you could do this:
但你可以这样做:
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
if( (units_changed & MINUTE_UNIT) != 0 ) {
/* Minutes changed */
}
if( (units_changed & DAY_UNIT) != 0 ) {
/* Days changed */
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.