Exploring Phonegap APIS
As I have already given you the list of APIs included in 3KRnHGDS, OHW's HxSORUH WKHP WR sHH wKDW EHnHfiW FDn EH obtained from each and how they can be included in your application to make it feature rich. These APIs give you the advantage of accessing native device features like the accelerometer, the compass, contacts, etc. Let’s start playing around with these APIs.
Accelerometer: This is a motion sensor that can detect a change in movement with respect to the device’s orientation in all the three axes. It allows you to change the position of an object in your application with every move of your device. Usage: This can be implemented in racing games. The methods for using the accelerometer are: getCurrentAcceleration()— Used to get the current acceleration along three axes x,y,z. watchAcceleration() - Used to get the acceleration with some regular time interval. clearWatch() - Used to stop the watch that you started with watchAcceleration() The arguments to pass through these methods are: 1. accelerometerSuccess() - This method will be called when we get successful acceleration from the device. accelerometerError() - This method will be called when we fail to receive acceleration from the device. accelerometerOptions – This will let you set the frequency for the time interval used in watchAcceleration() So let’s try to put these things up in the code to see it in action.
Replace index.html with accelerometer.html
3. The output of the above code is shown in Figure 4. Explanation: The code above shows the acceleration with an increasing timestamp.
Reference 1 in the code shows the document. addEventListener function, which depicts the wait for PhoneGap to load. References 2 and 3 depict that when PhoneGap is loaded, the deviceReady method will call startwatch(), i.e., the user defined method. Reference 4 shows that inside the startwatch(), there is a variable called the options ID, depicting the frequency for the timestamp. Reference 5 shows that the method watchAcceleration() fetches the current acceleration along the x,y and z axes. The arguments passed will be called at the time of success or failure. References 6 and T indicate that in case of success, the body element 'Waiting for acceleration' will be replaced by ‘acceleration’ with respect to x, y and z. Reference 8 shows that in case of an error, an alert will be shown with an error message. References 9 and 10 show that in the stopWatch() method, if the watch is not null, it means the watchAcceleration() method has been called before; so use clearwatch() to stop watching the accelerometer.