Titanium.App.iOS.BackgroundService
> Titanium.App.iOS.BackgroundService

A service that runs when the application is placed in the background.

A background service is created by Titanium.App.iOS.registerBackgroundService.

At creation, a local URL to a JavaScript file must be defined. The code it contains is executed each time the application is no longer in the foreground, along with all other services that have been registered in the same way. When this happens, all background services continue to run until one of the following occurs:

  • A service is stopped with the stop method.
  • The application resumes, at which points all background services are stopped.
  • The OS terminates the service for one of the reasons discussed in "Background Service Limitations", below.

A background service can invoke a Titanium.App.iOS.LocalNotification, which prompts users via a dialog to return to the application and provides a button that brings it back into the foreground.

Background Service Limitations

A background service is subject to limitations imposed by the operating system, such as

  • The OS limits the total amount of time a background service can run for after the application is paused, typically to no more than 10 minutes.
  • The OS may terminate the background service at any point to reclaim resources.

Examples

Background Services Example

Two background services are registered in the following application.

The first service logs a message every time the application is paused and then is stopped to release the service from memory. The service is not unregistered, and so will continue to be invoked.

The second creates an application property where it stores a run count value. For the first 4 times the application is paused, a local notification is invoked that gives the user the opportunity to bring the application back to the foreground. Once the run count reaches 5, the service is unregistered and is not invoked again until the application is relaunched.

app.js

var win1 = Ti.UI.createWindow({  
  title:'Background Services Example',
  backgroundColor:'#4186cd',
  modal:true
});

Ti.API.info('Registering background services');
var service = Ti.App.iOS.registerBackgroundService({url:'bg-service1.js'});
var service2 = Ti.App.iOS.registerBackgroundService({url:'bg-service2.js'});
Ti.API.info('*** Press home button to pause application ***');

win1.open();

bg-service1.js

Ti.API.info('bg-service1: service has been invoked once, and will now be stopped to release it from memory. ');
Ti.App.currentService.stop();

var listener = Ti.App.currentService.addEventListener('stop',function(){
  Ti.API.info('bg-service1: Although the service has been stopped, it is still registered and will be executed again on next pause');
  Ti.API.info('bg-service1: As all background services are automatically stopped on resume, it is not always necessary to explicitly stop a service');
});

bg-service2.js

var count = Ti.App.Properties.getInt('bg-svc2-count', 0);

if (count > 4){
  // reset count after 4 invocations
  count = 0;
}

count++;

Ti.App.Properties.setInt('bg-svc2-count', count);

Ti.API.info('bg-service2 has been run ' + count + ' times');

if (count > 4){
  Ti.App.currentService.unregister();
  var finalNotif = Ti.App.iOS.scheduleLocalNotification({
    alertBody:'bg-service2: As service has been invoked more than 4 times, it has been unregistered and will NOT run again. Relaunch the app to re-register it',
    date:new Date(new Date().getTime() + 1000) // 1 second after unregister
  });   
} else {
  var curNotif = Ti.App.iOS.scheduleLocalNotification({
    alertBody:'bg-service2: has been invoked ' + count + ' times. It is still registered and will run again when the app is transitioned to the background',
    date:new Date(new Date().getTime() + 1000) // 1 second after pause
  });   
}
  • 1.5
  • 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

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.App.iOS.BackgroundService
: StringCreation-Only
A local URL to a JavaScript file containing the code to run in the background. ...

A local URL to a JavaScript file containing the code to run in the background.

Default: none

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
Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Titanium.App.iOS.BackgroundService
( ) : String
Gets the value of the url property. ...

Gets the value of the url property.

Returns

  • String
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.App.iOS.BackgroundService
( url )
Sets the value of the url property. ...

Sets the value of the url property.

Parameters

  • url : String

    New value for the property.

Returns

  • void
Titanium.App.iOS.BackgroundService
( )
Stops the service from running during the current background session to conserve resources. ...

Stops the service from running during the current background session to conserve resources.

Returns

  • void
Titanium.App.iOS.BackgroundService
( )
Unregisters the background service. ...

Unregisters the background service.

Returns

  • void