| Platform | Since |
|---|---|
| Android | 1.5 |
The Titanium binding of an Android Service. Can be used to start/stop the service directly and listen for service-specific events. In the Javascript-based services you write, this can be referenced with Titanium.Android.currentService. You can create an instance of the service with Titanium.Android.createService.
Your service code in its own Javascript file. It reads the Titanium.Android.Intent which created it, to see what it should say besides "Hello World".
File: myservice.js:
var service = Titanium.Android.currentService; var intent = service.intent; var message = intent.getStringExtra("message_to_echo"); Titanium.API.info("Hello World! I am a Service. I have this to say: " + message);
Register the service in tiapp.xml:
<android xmlns:android="http://schemas.android.com/apk/res/android"> <services> <service url="myservice.js" type="interval"/> </services> </android>
Code in "regular" Titanium file to launch the service and listen for pause/resume events. Code also stops the service after its code runs 3 times.
var intent = Titanium.Android.createServiceIntent( { url: 'myservice.js' } ); // Service should run its code every 2 seconds. intent.putExtra('interval', 2000); // A message that the service should 'echo' intent.putExtra('message_to_echo', 'Titanium rocks!'); var service = Titanium.Android.createService(intent); service.addEventListener('resume', function(e) { Titanium.API.info('Service code resumes, iteration ' + e.iteration); }); service.addEventListener('pause', function(e) { Titanium.API.info('Service code pauses, iteration ' + e.iteration); if (e.iteration === 3) { Titanium.API.info('Service code has run 3 times, will now stop it.'); service.stop(); } }); service.start();
Console Output:
[INFO] [29,1942] Service code resumes, iteration 1 [INFO] [70,2029] Hello World! I am a Service. I have this to say: Titanium rocks! [INFO] [3,2070] Service code pauses, iteration 1 [INFO] [2,3915] Service code resumes, iteration 2 [INFO] [31,3961] Hello World! I am a Service. I have this to say: Titanium rocks! [INFO] [5,3968] Service code pauses, iteration 2 [INFO] [2,5917] Service code resumes, iteration 3 [INFO] [27,5961] Hello World! I am a Service. I have this to say: Titanium rocks! [INFO] [16,5980] Service code pauses, iteration 3 [INFO] [1,5981] Service code has run 3 times, will now stop it.
| Name | Summary |
|---|---|
| addEventListener |
Adds the specified callback as an event listener for the named event. |
| fireEvent |
Fires a synthesized event to any registered listeners. |
| removeEventListener |
Removes the specified callback as an event listener for the named event. |
| start |
Start the Service. Effective only if this instance of |
| stop |
Stop this running instance of the Service. |
| Name | Type | Summary |
|---|---|---|
| intent | Titanium.Android.Intent |
The |
| serviceInstanceId | Number |
A service can be started more than once -- this number (based on an incrementing integer) indicates which "start number" in the sequence the current service instance is. read-only |
| Name | Summary |
|---|---|
| pause |
For Javascript-based Services which you create, |
| resume |
For Javascript-based Services which you create, |
| start |
Fired when the bound Service instance starts. Bound service instances are created via |
| stop |
Fired when the bound Service instance stops, meaning |