Service Application: Creating Background Applications
This tutorial demonstrates how you can manage service applications.
Warm-up
Become familiar with the Service Application API basics by learning about:
-
Listening to Events
Register and set callbacks for application state change events and system events.
Listening to Events
To listen to events:
-
To use the functions and data types of the Service Application API (in mobile and wearable applications), include the <service_app.h> header file in your application:
#include <service_app.h>
-
Add callbacks for application state change events:
-
Add a callback for initializing the service application. This callback is called when the application is launched.
Use the callback to write the necessary initialization code, such as setting up the dbus connection.
The callback returns a Boolean value. If there is a critical error during the launch, the return is false, thereby cancelling the launch. Otherwise, the return is true.
The callback receives user data.
bool service_app_create(void *data) { dlog_print(DLOG_DEBUG, LOG_TAG, "%s", __func__); return true; }
-
Add a callback for terminating the service application. This callback is called when the application terminates.
Use the callback to release all resources, especially any allocations and shared resources used by other applications.
The callback returns nothing: Its return type is void.
The service_app_exit() function quits the application's main loop internally.
void __service_app_terminate(void *data) { dlog_print(DLOG_DEBUG, LOG_TAG, "%s", __func__); service_app_exit(); return; }
-
Add a control callback for handling service requests from other applications. This callback is called when the service application receives an app_control request from another application.
void __service_app_control (app_control_h app_control, void *data) { dlog_print(DLOG_DEBUG, LOG_TAG, "%s", __func__); service_app_exit(); return; }
-
-
Add callbacks for system events:
-
Add the low memory callback for handling the low memory event. This callback is called when the device is low on memory.
void service_app_low_memory_callback(void *data) { dlog_print(DLOG_DEBUG, LOG_TAG, "%s", __func__); service_app_exit(); return; }
-
Add the low battery callback for handling the low battery event. This callback is called when the device is low on battery power.
void service_app_low_battery_callback(void *data) { dlog_print(DLOG_DEBUG, LOG_TAG, "%s", __func__); service_app_exit(); return; }
-
-
Set the callbacks in the service_app_event_callback_s structure. This structure is passed to the function that starts the service application.
int main(int argc, char* argv[]) { appdata_s ad = {0,}; service_app_lifecycle_callback_s event_callback = {0,}; dlog_print(DLOG_DEBUG, LOG_TAG, "%s", __func__); event_callback.create = service_app_create; event_callback.terminate = service_app_terminate; event_callback.app_control = service_app_control; dlog_print(DLOG_DEBUG, LOG_TAG, "service_app_main() is called."); return service_app_main(argc, argv, &event_callback, &ad); }