Service Application: Creating a Service Application
This tutorial demonstrates how you can create a service application and implement specific features within the service application.
This feature is supported in wearable applications only.
Warm-up
Become familiar with the service application basics by learning about:
-
Creating a Service Application
Create a service application by writing key components.
-
Packaging a Service Application
Package a service application.
-
Launching a Service Application
Launch a service application.
-
Terminating a Service Application
Terminate a service application
Creating a Service Application
Learning how to create a service application is a basic application management skill:
The service application consists of a number of callbacks. To write a service application, you must implement the predefined callbacks for their purpose and register them using the CommonJS Modules API.
- Create the service entry point with the onStart() callback.
The callback is invoked when the service is launched. Within the callback, you can prepare resources and initialize whatever the service application needs during the execution.
module.exports.onStart = function() { console.log("service start"); var remoteMsgPort = tizen.messageport.requestRemoteMessagePort("websvcapp0.WebServiceApplication", "SERVICE_SAMPLE1"); var localMsgPort = tizen.messageport.requestLocalMessagePort("SERVICE_SAMPLE2"); function onreceived(data, remoteMsgPort) { for (var i = 0; i < data.length; i++) { if (data[i].value == "SERVICE_EXIT") { localMsgPort.removeMessagePortListener(watchId); tizen.application.getCurrentApplication().exit(); } } } var watchId = localMsgPort.addMessagePortListener(onreceived); }
- Write the request handler with the onRequest() callback.
The callback is invoked to handle incoming service requests. Within the callback, write code for each request from other applications and the platform. To obtain the request, use the getRequestedAppControl() function in the Application API.
module.exports.onRequest = function() { var reqAppControl = tizen.application.getCurrentApplication().getRequestedAppControl(); if (reqAppControl) { if (reqAppControl.appControl.operation == "http://tizen.org/appcontrol/operation/service") { try { tizen.systeminfo.addPropertyValueChangeListener("DEVICE_ORIENTATION", onDeviceOrientationSuccess); } } } }
- Write the termination with the onExit() callback.
The callback is invoked when the service is about to be stopped. All resources can be cleared and backed up within the callback.
module.exports.onExit = function() { console.log("service terminate"); }
Packaging a Service Application
Learning how to package a service application is a basic application management skill:
A Web application package can contain 1 Web UI application and several service applications. Each application in the Web application package shares the same package ID and has a unique application ID. In the following example, you can use the <tizen:application> element to contain information for the Web UI application. The <tizen:service> element is used to contain information about the service application. The UI application and the service application have the same package ID and different application IDs.
- Define the service in the config.xml file.
The <tizen:service> element allows you to define the characteristics of the service application. For example, you can specify the name, icon, and starting JavaScipt file of the service application.
<?xml version="1.0"encoding="TF-8"> <widget xmlns="http://www.w3.org/ns/widgets" xmlns:tizen=http://tizen.org/ns/widgets id="http://yourdomain/WebServiceApplication" version="1.0.0" viewmodes="maximized"> <tizen:application id="websvcapp0.WebServiceApplication" package="websvcapp0" required_version="2.3" /> <content src="index.html" /> <feature name="http://tizen.org/feature/screen.size.all" /> <icon src="icon.png" /> <name>WebServiceApplication</name> <tizen:service id="websvcapp0.service1" auto-restart="true" on-boot="false"> <tizen:content src="service/service1.js" /> <tizen:name>WebServiceApplication1</tizen:name> <tizen:icon src="icon1.png" /> <tizen:description>WebServiceApplication1</tizen:description> </tizen:service> </widget>
Launching a Service Application
Learning how to launch a service application is a basic application management skill:
- Launch by other applications
The Web application launches a service application by calling the launch() or launchAppControl() function with the service application ID:
tizen.application.launchAppControl(new tizen.ApplicationControl("http://tizen.org/appcontrol/operation/service"), "websvcapp0.service1", function() {console.log("Launch Service succeeded"); }, function(e) {console.log("Launch Service failed : " + e.message);});
- Launch by the system
A service application can start automatically if the on-boot attribute is set to true. This requires partner-level certification.
<tizen:service id="websvcapp0.service1" on-boot="true">
Terminating a Service Application
Learning how to terminate a service application is a basic application management skill:
-
The service application can terminate itself when it receives a particular request. The following example code uses the Message Port API to send such a request to the service application.
The application sends a message by calling the sendMessage() function.
var remoteMsgPort = tizen.messageport.requestRemoteMessagePort("websvcapp0.service1", "SERVICE_SAMPLE2"); remoteMsgPort.sendMessage([{ key : "key", value : "SERVICE_EXIT" }]);
- The service application can terminate itself by calling the exit() function after getting a signal through the message port:
var localMsgPort = tizen.messageport.requestLocalMessagePort("SERVICE_SAMPLE2"); function onreceived(data, remoteMsgPort) { for (var i = 0; i < data.length; i++) { if (data[i].value == "SERVICE_EXIT") { localMsgPort.removeMessagePortListener(watchId); tizen.application.getCurrentApplication().exit(); } } } var watchId = localMsgPort.addMessagePortListener(onreceived);