Titanium.Android.Service
> Titanium.Android.Service

Android application component that executes in the background.

A service is a component started by an application that runs in the background. The service does not have any application UI associated with it, so the user does not directly interact with it, only your application.

The Titanium SDK gives you the ability to write your own Android Services using JavaScript. The service executes your JavaScript code at intervals you specify. Note that the service may stop running if the application is killed.

To create a service:

  1. Write the JavaScript code you want the service to execute in a separate file. The service can execute any Titanium APIs but you should only use non-UI APIs.
  2. Register the service in your tiapp.xml file. Refer to the example below.
  3. Create a service intent by passing the JavaScript file to the Titanium.Android.createServiceIntent method and set the interval to run the code using the intent's putExtra() method.
  4. Pass the Intent object to either the Titanium.Android.createService method to create a Service object (bound service), where the application can manage the service by invoking methods and binding callbacks on the object, or to the Titanium.Android.startService method to start the service (started service) and the service manages itself.

Use the Titanium.Android.Service API to manage the service.

To get a reference to the Service inside the JavaScript service code, use the Titanium.Android.currentService property to retrieve a reference to the service, then invoke the Titanium.Android.Service APIs on the Service object.

Further Reading:

Examples

Hello World Service

This example shows how to create a service in JavaScript. Your service code is included in its own JavaScript file.

In this example, the service reads data from 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:

<ti:app>
    <android xmlns:android="http://schemas.android.com/apk/res/android">
        <services>
            <service url="myservice.js" type="interval"/>
        </services>
    </android>
</ti:app>

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.
  • 1.5
Defined By

Properties

apiName : Stringreadonly

The name of the API that this proxy corresponds to.

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Button returns Ti.UI.Button.

  • 3.2.0
  • 3.2.0
  • 3.2.0
Indicates if the proxy will bubble an event to its parent. ...

Indicates if the proxy will bubble an event to its parent.

Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.

Default: true

  • 3.0.0
  • 3.0.0
  • 3.0.0
Titanium.Android.Service
intent : Titanium.Android.Intentreadonly

The intent used to start or bind to the Service.

The intent used to start or bind to the Service.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.

  • 3.6.0
Titanium.Android.Service
: Numberreadonly
A service can be started more than once -- this number (based on an incrementing integer) indicates which "start numb...

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.

Defined By

Methods

Adds the specified callback as an event listener for the named event. ...

Adds the specified callback as an event listener for the named event.

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to invoke when the event is fired.

Returns

  • void
Applies the properties to the proxy. ...

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • props : Dictionary

    A dictionary of properties to apply.

Returns

  • void
Fires a synthesized event to any registered listeners. ...

Fires a synthesized event to any registered listeners.

Parameters

  • name : String

    Name of the event.

  • event : Dictionary

    A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Returns

  • void
Gets the value of the apiName property. ...

Gets the value of the apiName property.

  • 3.2.0
  • 3.2.0
  • 3.2.0

Returns

  • String
Gets the value of the bubbleParent property. ...

Gets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Returns

  • Boolean
Titanium.Android.Service
( ) : Titanium.Android.Intent
Gets the value of the intent property. ...

Gets the value of the intent property.

Returns

Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Titanium.Android.Service
( ) : Number
Gets the value of the serviceInstanceId property. ...

Gets the value of the serviceInstanceId property.

Returns

  • Number
Removes the specified callback as an event listener for the named event. ...

Removes the specified callback as an event listener for the named event.

Multiple listeners can be registered for the same event, so the callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to remove. Must be the same function passed to addEventListener.

Returns

  • void
Sets the value of the bubbleParent property. ...

Sets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • bubbleParent : Boolean

    New value for the property.

Returns

  • void
Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void
Titanium.Android.Service
( )
Starts the Service. ...

Starts the Service.

Effective only if this instance of Titanium.Android.Service was created with Titanium.Android.createService.

Returns

  • void
Titanium.Android.Service
( )
Stops this running instance of the Service. ...

Stops this running instance of the Service.

Returns

  • void
Defined By

Events

Titanium.Android.Service
For Javascript-based services that you create, pause fires after each time the JavaScript code executes. ...

For Javascript-based services that you create, pause fires after each time the JavaScript code executes.

The resume and pause events occur in pairs, with resume firing just before your JavaScript service code executes, and pause just after.

Properties

  • iteration : String

    Incrementing integer indicating which iteration of an interval-based Service is pausing. For example, if you have an interval-based Service running every 10 seconds, iteration 3 would occur at about 30 seconds after you start the instance (assuming your service code runs quickly).

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

    True if the event will try to bubble up if possible.

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •  
Titanium.Android.Service
For JavaScript-based Services which you create, resume fires each time the JavaScript code executes. ...

For JavaScript-based Services which you create, resume fires each time the JavaScript code executes.

For example, if your Service runs on an interval of 10000 (10 seconds), you would expect to see resume fired every 10 seconds, just as the JavaScript service code you wrote is about to execute.

Properties

  • iteration : String

    Incrementing integer indicating which iteration of an interval-based Service is pausing. For example, if you have an interval-based Service running every 10 seconds, iteration 3 would occur at about 30 seconds after you start the instance (assuming your service code runs quickly).

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

    True if the event will try to bubble up if possible.

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •  
Titanium.Android.Service
Fired when the bound service instance starts. ...

Fired when the bound service instance starts.

Bound service instances are created via Titanium.Android.createService.

Properties

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

    True if the event will try to bubble up if possible.

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •  
Titanium.Android.Service
Fired when the bound service instance stops. ...

Fired when the bound service instance stops.

The service stops when stop or Titanium.Android.stopService is called and there are no more bound, un-stopped clients.

Properties

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

    True if the event will try to bubble up if possible.

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •  
Titanium.Android.Service
Fired when the task that comes from the service's application has been removed. ...

Fired when the task that comes from the service's application has been removed.

This event is fired if the service is currently running and the user has removed a task that comes from the service's application, eg. the user swipes the application away from the recent applications list. It only works for unbound service which is started using Titanium.Android.startService.

Properties

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

    True if the event will try to bubble up if possible.

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •