您可以尝试为此目的使用警报 API。请按照以下步骤操作:
- 首先创建一个Watch Application 和一个Service Application。打包两个应用程序并构建。通过this link 进行包装。
-
现在在表盘应用程序中使用以下代码 sn-p 在特定时间(在您的情况下为 1 小时)后触发警报。
bool init_alert(){
int ret;
int DELAY = 3;
int REMIND = 3600; //alert after every 1hr.
int alarm_id;
app_control_h app_control = NULL;
ret = app_control_create(&app_control);
ret = app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
ret = app_control_set_app_id(app_control, "Service_App_ID");
ret = alarm_schedule_after_delay(app_control, DELAY, REMIND, &alarm_id);
return true;}
在表盘应用清单文件中使用以下权限:
http://tizen.org/privilege/alarm.get
http://tizen.org/privilege/alarm.set
-
现在在服务应用程序的 service_app_control() 函数中,使用以下代码创建警报:
void service_app_control(app_control_h app_control, void *data){
dlog_print(DLOG_INFO, LOG_TAG, "Hourly Alert Here");
notification = notification_create(NOTIFICATION_TYPE_NOTI);
int ret =0;
ret = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_TITLE, "Hourly Alert!!",
NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
ret = notification_post(notification);
notification_free(notification);
return;}
在服务应用清单文件中使用以下权限:
http://tizen.org/privilege/notification
欲知详情,请通过this link。
编辑:1
您可以在this link 中找到此问题的原因。请浏览备注和另见部分。我之前已经发布了 2.3.2 版的答案。这就是为什么我没有收到任何错误。
请按照下面的方法。就我而言,它适用于 3.0 版。
第 1 步: 取一个计数器,首先将其设置为 0,并在每次时钟为 0 秒时递增,如下所示:
watch_time_get_second(watch_time, &second);
if(second == 0) count++;
第 2 步: 现在,检查计数器值是否等于您所需的值,例如如果您希望每 3 分钟间隔后发出警报,则可以检查它是否等于 3,然后创建如下通知:
if(count == 3){
dlog_print(DLOG_INFO, LOG_TAG, "Hourly Alert");
notification = notification_create(NOTIFICATION_TYPE_NOTI);
int ret =0;
ret = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_TITLE, "Hourly Alert!!",
NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
ret = notification_post(notification);
notification_free(notification);
count = 0;
}
最后,将计数器设置为 0,准备好为下一个间隔计数。找到下面的完整代码结构以便更好地理解:
#include <notification.h>
static notification_h notification = NULL;
int count = 0;
static void
update_watch(appdata_s *ad, watch_time_h watch_time, int ambient)
{
char watch_text[TEXT_BUF_SIZE];
int hour24, minute, second;
if (watch_time == NULL)
return;
watch_time_get_hour24(watch_time, &hour24);
watch_time_get_minute(watch_time, &minute);
watch_time_get_second(watch_time, &second);
if (!ambient) {
if(second == 0) count++;
if(count == 3){
dlog_print(DLOG_INFO, LOG_TAG, "Hourly Alert");
notification = notification_create(NOTIFICATION_TYPE_NOTI);
int ret =0;
ret = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_TITLE, "Hourly Alert!!",
NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
ret = notification_post(notification);
notification_free(notification);
count = 0;
}
dlog_print(DLOG_INFO, LOG_TAG, "cnt = %d", count);
snprintf(watch_text, TEXT_BUF_SIZE, "<align=center>Hello<br/>%02d:%02d:%02d</align>",
hour24, minute, second);
} else {
snprintf(watch_text, TEXT_BUF_SIZE, "<align=center>Hello Watch<br/>%02d:%02d</align>",
hour24, minute);
}
elm_object_text_set(ad->label, watch_text);
}
注意:不要忘记添加通知权限。