Event Broadcast and Subscription
The application can broadcast its own events to all listeners, and subscribe to events. The events can be either predefined system events from the platform (only platform modules can broadcast system events) or user-defined events (broadcast by UI and service applications).
The main features of the Event API are:
-
Event publication
You can publish an event using the
event_publish_app_event()andevent_publish_trusted_app_event()functions. Theevent_publish_trusted_app_event()function publishes a trusted event which can only be received by the application that has the same signature as the publishing application. -
Event subscription
You can subscribe to an event using the
event_add_event_handler()function. When no longer needed, unsubscribe the event with theevent_remove_event_handler()function. -
Launch-On-Events
Service applications can be launched when a desired target event occurs.
The application can be suspended while in the background, causing a pause in event handling. Since the application cannot receive events in the suspended state, they are all delivered in series after the application exits the suspended state. Consider how to manage this situation and prevent the application from being flooded with events:
- To handle events in the background without going to a suspended state, declare a background category.
- To avoid receiving any events that are triggered while the application is suspended, remove the event handler before entering the suspended state and add it back after exiting the suspended state. You can manage the event handler addition and removal in the
APP_EVENT_SUSPENDED_STATE_CHANGEDevent callback, which is triggered each time the application enters and exist the suspended state.
Prerequisites
To enable your application to use the event functionality:
-
To use the functions and data types of the Event API, include the
<app_event.h>header file in your application:#include <app_event.h> -
To use Launch-On-Events in your application, define the
http://tizen.org/appcontrol/operation/launch_on_eventoperation in thetizen-manifest.xmlfile.The URI name for the operation represents the event name in the Launch-On-Event format (
event://{Event_Name}).<app-control> <operation name="http://tizen.org/appcontrol/operation/launch_on_event"/> <uri name="event://tizen.system.event.battery_charger_status"/> </app-control>
Publishing an Event
To publish an event to all listeners:
-
Create the callback for handling the event:
static void user_event_cb(const char *event_name, bundle *event_data, void *user_data) { dlog_print(DLOG_INFO, LOG_TAG, "user_event_cb: %s \n", event_name); return; } -
Register the event handler and create the bundle for handling the event data:
int ret = EVENT_ERROR_NONE; event_handler_h event_handler; bundle *event_data = NULL; ret = event_add_event_handler("event.org.tizen.senderapp.user_event", user_event_cb, "CUSTOM_EVENT_KEY", &event_handler); if (ret != EVENT_ERROR_NONE) dlog_print(DLOG_ERROR, LOG_TAG, "err: [%d]", ret); event_data = bundle_create(); ret = bundle_add_str(event_data, user_data_key, user_data); -
Use the
event_publish_app_event()function to publish the event:ret = event_publish_app_event("event.org.tizen.senderapp.user_event", event_data); if (ret != EVENT_ERROR_NONE) dlog_print(DLOG_ERROR, LOG_TAG, "err: [%d]", ret); -
When no longer needed, free the bundle:
ret = bundle_free(event_data);
Subscribing to an Event
To subscribe to a predefined system event or user-defined event:
-
Add an event handler.
One event can have multiple event handlers, and one handler can be registered multiple times.
-
Add an event handler for a system event:
static void battery_event_callback(const char *event_name, bundle *event_data, void *user_data) { /* event_name is the event name */ dlog_print(DLOG_INFO, LOG_TAG, "event_name is [%s]", event_name); /* event_data is the event data, its type is bundle */ char *battery_level_status = NULL; bundle_get_str(event_data, EVENT_KEY_BATTERY_LEVEL_STATUS, &battery_level_status); } event_handler_h handler; /* Register the event handler */ int ret = event_add_event_handler(SYSTEM_EVENT_BATTERY_LEVEL_STATUS, (event_cb)battery_event_cb, user_data, &handler); if (ret != EVENT_ERROR_NONE) dlog_print(DLOG_ERROR, LOG_TAG, "err: [%d]", ret); -
Add an event handler for a user-defined event:
When defining an event name for a user event (such as
event.org.tizen.senderapp.user_event), the name format isevent.{sender appid}.{user-defined name}. The{user-defined name}must:- Contain only the ASCII characters “[A-Z][a-z][0-9]_” and not begin with a digit.
- Not contain the ‘.’ (period) character.
- Not exceed the maximum name length (127 bytes).
- Be at least 1 byte in length.
ret = event_add_event_handler("event.org.tizen.senderapp.user_event", utc_event_cb_with_valid_check, "CUSTOM_EVENT_KEY", &event_handler); if (ret != EVENT_ERROR_NONE) dlog_print(DLOG_ERROR, LOG_TAG, "err: [%d]", ret);
-
-
When no longer needed, remove the event handler.
A registered handler can be removed when application is running, and all registered handlers are removed when the application is terminated.
ret = event_remove_event_handler(handler); if (ret != EVENT_ERROR_NONE) dlog_print(DLOG_ERROR, LOG_TAG, "err: [%d]", ret);
Managing Launch-On-Events
To register an interest in a Launch-On-Event, define the http://tizen.org/appcontrol/operation/launch_on_event operation in the tizen-manifest.xml file.
Note
Only service applications can register and receive Launch-On-Events.
The Launch-On-Event operation cannot be requested using the
app_control_send_launch_request()function, unlike other application control operations.
The following table shows the system events that support Launch-On-Event.
Table: System events supporting Launch-On-Event
| Name | Condition |
|---|---|
tizen.system.event.battery_charger_status |
When the charger state is "connected". |
tizen.system.event.usb_status |
When the USB state is "connected". |
tizen.system.event.earjack_status |
When the earjack status is "connected". |
tizen.system.event.incoming_msg |
When the msg_type and msg_id exist. |
tizen.system.event.wifi_state |
When the Wi-Fi state is "connected". |
To receive the Launch-On-Event:
static void
*app_control(app_control_h app_control, void *data)
{
char *event_uri = "event://tizen.system.event.battery_charger_status";
char *operation;
char *uri;
char *event_value;
ret = app_control_get_operation(app_control, &operation);
if (ret == APP_CONTROL_ERROR_NONE && operation &&
strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT) == 0) {
ret = app_control_get_uri(app_control, &uri);
if (ret == APP_CONTROL_ERROR_NONE && uri) {
if (strncmp(uri, event_uri, strlen(event_uri) + 1) == 0) {
ret = app_control_get_extra_data(app_control, "battery_charger_status", &event_value);
if (ret == APP_CONTROL_ERROR_NONE && event_value)
free(event_value);
/* Use event_add_event_handler() for further event subscriptions here */
}
free(uri);
}
free(operation);
}
}
The application can get the event name and data in the first app_control_cb() callback, which is called after the application state changes to created.
Platform Event Types
The following list shows the events of modules:
-
capi-system-device
-
battery
Event name Event data Key Event data Value Condition Notes tizen.system.event.battery_charger_statusbattery_charger_status- "disconnected": Charger is not connected
-"connected": Charger is connected
-"charging": Charging is enabled
-"discharging": Charging is disabled (for example, 100% full state)When the charger has been connected or disconnected, or when the charging state has changed (charging or not). If there is an earlier occurrence regarding this event, you receive the event as soon as you register an event handler for this event. You can use this earlier event data as the initial value. tizen.system.event.battery_level_statusbattery_level_status- "empty"
-"critical"
-"low"
-"high"
-"full"When the battery level has changed. You can get the current value with the device_battery_get_level_status()function.
-
-
deviced
-
usb
Event name Event data Key Event data Value Condition Notes tizen.system.event.usb_statususb_status- "disconnected": USB cable is not connected
-"connected": USB cable is connected, but the service is not available
-"available": USB service is available (for example, mtp or SDB)When the USB cable has been connected or disconnected, or when the USB service state has changed. You can get the current value with the RUNTIME_INFO_KEY_USB_CONNECTEDkey. -
earjack
Event name Event data Key Event data Value Condition Notes tizen.system.event.earjack_statusearjack_status- "disconnected": Earjack is not connected
-"connected": Earjack is connectedWhen the earjack connection state has changed. You can get the current value with the RUNTIME_INFO_KEY_AUDIO_JACK_STATUSkey. -
display
Event name Event data Key Event data Value Condition Notes tizen.system.event.display_statedisplay_state- "normal": Display on, normal brightness
-"dim": Display on, dimmed brightness
-"off": Display offWhen the display state has changed. You can get the current value with the device_display_get_state()function.
-
-
systemd
-
system
Event name Event data Key Event data Value Condition Notes tizen.system.event.boot_completedN/A N/A When the platform has completed booting. You can treat the initial value as falsebefore you receive this event. If the application is already in a boot-completed state before you register an event handler, you receive the event as soon as you register the event handler.tizen.system.event.system_shutdownN/A N/A When the system power-off has been started. You can treat the initial value as falsebefore you receive this event. If the application is already in a shutting-down state before you register an event handler, you receive the event as soon as you register the event handler.
-
-
resourced
-
ram memory
Event name Event data Key Event data Value Condition Notes tizen.system.event.low_memorylow_memory- "normal": Available > 200MB
-"soft_warning": 100MB < available <= 200MB
-"hard_warning": Available <= 100MB
Note
The above numbers can vary depending on the total RAM size of the target device.When the size of available memory has changed. If there is an earlier occurrence regarding this event, you receive the event as soon as you register an event handler for this event. You can use this earlier event data as the initial value.
-
-
network
-
connectivity
Event name Event data Key Event data Value Condition Notes tizen.system.event.wifi_statewifi_state- "on": Wi-Fi on
-"off": Wi-Fi off
-"connected": Wi-Fi connection establishedWhen the Wi-Fi state has changed. You can get the current value with the connection_get_wifi_state()function.tizen.system.event.bt_statebt_state- "off": Legacy Bluetooth off
-"on": Legacy Bluetooth onWhen the Bluetooth state has changed. You can get the current value with the bt_adapter_get_state()function.tizen.system.event.bt_statebt_le_state- "off": LE function off
-"on": LE function onWhen Bluetooth LE state has changed. - tizen.system.event.bt_statebt_transfering_state- "non_transfering": Idle state
-"transfering": File is transferringWhen the file transfer state has changed. -
-
-
libslp-location
-
location
Event name Event data Key Event data Value Condition Notes tizen.system.event.location_enable_statelocation_enable_state- "disabled": Location disabled
-"enabled": Location enabledWhen the location-enable_statehas been changed, for example, by the user toggling the location setting in the settings menu or quick panel.You can get the current value with the location_manager_is_enabled_method()function.tizen.system.event.gps_enable_stategps_enable_state- "disabled": GPS disabled
-"enabled": GPS enabledWhen the gps-enable_statehas changed.You can get the current value with the location_manager_is_enabled_method()function.tizen.system.event.nps_enable_statenps_enable_state- "disabled": NPS disabled
-"enabled": NPS enabledWhen the NPS setting has been changed, for example, by the user toggling the location settings. You can get the current value with the location_manager_is_enabled_method()function.
-
-
msg-service
-
message
Event name Event data Key Event data Value Condition Notes tizen.system.event.incoming_msgmsg_type- "sms": SMS-type message
-"push": Push-type message
-"cb": Cb-type messageWhen an SMS, push, or CB message has been received. - tizen.system.event.incoming_msgmsg_idmsg_id: Message ID of the received message (string of the unsignedinttype value)- -
-
-
alarm-manager
-
time
Event name Event data Key Event data Value Condition Notes tizen.system.event.time_changedN/A N/A When the system time setting has changed. You can get the current value with the alarm_get_current_time()function.
-
-
setting
-
time
Event name Event data Key Event data Value Condition Notes tizen.system.event.time_zonetime_zoneThe value of this key is the time zone value of the time zone database, for example, “Asia/Seoul”, “America/New_York”. For more information, see the IANA Time Zone Database. When the time zone has changed. You can get the current value with the SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONEkey. -
locale
Event name Event data Key Event data Value Condition Notes tizen.system.event.hour_formathour_format- "12"
-"24"When the hour_formathas changed, for example, by the user toggling the date and time settings for the 24-hour clock (where OFF stands for the 12-hour clock).You can get the current value with the SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOURkey.tizen.system.event.language_setlanguage_setThe value of this key is the full name of the locale, for example, ko_KR.UTF8for Korean anden_US.UTF8for American English. For more information, see the Linux locale information.When the language_sethas changed.You can get the current value with the SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGEkey.tizen.system.event.region_formatregion_formatThe value of this key is the full name of the locale, for example, ko_KR.UTF8for the Korean region format anden_US.UTF8for the USA region format. For more information, see the Linux locale information.When the region_formathas changed.You can get the current value with the SYSTEM_SETTINGS_KEY_LOCALE_COUNTRYkey. -
sound
Event name Event data Key Event data Value Condition Notes tizen.system.event.silent_modesilent_mode- "on"
-"off"When the ringtone has been changed to 0 or another mode. For example, if the call slider has been changed to 0, silent_modeis"on". Otherwise,silent_modeis"off".You can get the current value with the SYSTEM_SETTINGS_KEY_SOUND_SILENT_MODEkey. -
vibration
Event name Event data Key Event data Value Condition Notes tizen.system.event.vibration_statevibration_state- "on"
-"off"When the vibration state has changed. You can get the current value with the RUNTIME_INFO_KEY_VIBRATION_ENABLEDkey. -
screen
Event name Event data Key Event data Value Condition Notes tizen.system.event.screen_autorotate_statescreen_autorotate_state- "on"
-"off"When the screen_autorotate_statehas been changed, for example, by the user toggling the display settings.You can get the current value with the SYSTEM_SETTINGS_KEY_DISPLAY_SCREEN_ROTATION_AUTOkey. -
mobile
Event name Event data Key Event data Value Condition Notes tizen.system.event.mobile_data_statemobile_data_state- "on"
-"off"When the mobile_data_statehas been changed, for example, by the user toggling the network settings.You can get the current value with the SYSTEM_SETTINGS_KEY_3G_DATA_NETWORK_ENABLEDkey.tizen.system.event.data_roaming_statedata_roaming_state- "on"
-"off"When the data_roaming_statehas been changed, for example, by the user toggling the network settings.You can get the current value with the RUNTIME_INFO_KEY_DATA_ROAMING_ENABLEDkey. -
font
Event name Event data Key Event data Value Condition Notes tizen.system.event.font_setfont_setThe value of this key is the font name of the string type by font-config. When the font has changed. You can get the current value with the SYSTEM_SETTINGS_KEY_FONT_TYPEkey.
-
Related Information
- Dependencies
- Since Tizen 2.4
- API References