O'Reilly logo

PhoneGap by Andrew Lunny

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – Detecting shakes

Unlike the geolocation API, a single reading of the device's accelerometer is not all that interesting. In this example, we're going to poll the accelerometer for a period, to see how the readings change as we shake the device.

You will not get very far with the accelerometer on a desktop web browser or an emulator. This is the first API we've encountered that really requires a device at hand to measure your progress. You can refer back to Chapter 1 for details on how to get a PhoneGap application loaded onto your device.

  1. Let's go back to our Postcard application, and set up a watch on the accelerometer. To do this, we'll have to listen for the deviceready event and, once it fires, call watchAccelerometer:
    x$(document).on('deviceready', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required