Skip to content

Titanium.Accelerometer

The top-level Accelerometer module, used to determine the device's physical position.

An accelerometer is a hardware unit integrated into a mobile device, that detects when the device has moved, and returns its new orientation in a three-dimensional space. With its single update event, this module provides an interface to access the output positional data.

An accelerometer needs to be switched on in order for it to report to the operating system, which consumes a lot of power that will deplete the battery over time. This is why it is recommended that the accelerometer is switched off when not in use.

The accelerometer may be switched on and off by simply adding and removing the update event listener function. See the example for a demonstration.

Extends: Titanium.Module · Since: 0.1

Properties #

apiName#

extended

Type: String

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.

bubbleParent#

extended

Type: Boolean

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.

lifecycleContainer#

extended

Type: Titanium.UI.Window, Titanium.UI.TabGroup

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.

Methods #

addEventListener #

extended

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

Parameters:
NameTypeSummaryOptional
nameStringName of the event.No
callbackCallback<Titanium.Event>Callback function to invoke when the event is fired.No

applyProperties #

extended

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.

Parameters:
NameTypeSummaryOptional
propsDictionaryA dictionary of properties to apply.No

fireEvent #

extended

Fires a synthesized event to any registered listeners.

Parameters:
NameTypeSummaryOptional
nameStringName of the event.No
eventDictionaryA dictionary of keys and values to add to the Titanium.Event object sent to the listeners.Yes

removeEventListener #

extended

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:
NameTypeSummaryOptional
nameStringName of the event.No
callbackCallback<Titanium.Event>Callback function to remove. Must be the same function passed to addEventListener.No

Events #

update #

Fired when the accelerometer changes.

Event Properties:
NameTypeSummary
timestampNumberReference timestamp since the previous change. This is not a valid timestamp and should
simply be used to determine the number of milliseconds between events.
yNumberCurrent y axis of the device.
xNumberCurrent x axis of the device.
zNumberCurrent z axis of the device.

Examples #

Basic Accelerometer Event

Adds an accelerometer update event listener, and displays the x, y and z axis in the window when the device is moved. On Android, the accelerometer is switched off and on when the application is paused and resumed.

var win = Ti.UI.createWindow({
  backgroundColor: 'white',
  exitOnClose:true,
  layout: 'vertical'
});

opts = {
  color:'black',
  font:{fontSize:20},
  text:'-',
  top:20, left:10,
  width:300
};
var labelTimestamp = Ti.UI.createLabel(opts);
win.add(labelTimestamp);
var labelx = Ti.UI.createLabel(opts);
win.add(labelx);
var labely = Ti.UI.createLabel(opts);
win.add(labely);
var labelz = Ti.UI.createLabel(opts);
win.add(labelz);
win.open();

var accelerometerCallback = function(e) {
  labelTimestamp.text = 'timestamp: ' + e.timestamp;
  labelx.text = 'x: ' + e.x;
  labely.text = 'y: ' + e.y;
  labelz.text = 'z: ' + e.z;
};

if (Ti.Platform.model === 'Simulator' || Ti.Platform.model.indexOf('sdk') !== -1 ) {
  alert('Accelerometer does not work on a virtual device');
} else {
  Ti.Accelerometer.addEventListener('update', accelerometerCallback);
  if (Ti.Platform.name === 'android') {
    win.addEventListener("open", function(e) {
      win.activity.addEventListener("pause", function() {
        Ti.API.info("removing accelerometer callback on pause");
        Ti.Accelerometer.removeEventListener('update', accelerometerCallback);
      });
      win.activity.addEventListener("resume", function() {
        Ti.API.info("adding accelerometer callback on resume");
        Ti.Accelerometer.addEventListener('update', accelerometerCallback);
      });
    });
  }
}

Titanium SDK Documentation