- Cordova - Best Practices
- Cordova - Whitelist
- Cordova - Vibration
- Cordova - Splash Screen
- Cordova - Network Information
- Cordova - Media Capture
- Cordova - Media
- Cordova - InAppBrowser
- Cordova - Globalization
- Cordova - Geolocation
- Cordova - File Transfer
- Cordova - File System
- Cordova - Dialogs
- Cordova - Device Orientation
- Cordova - Accelerometer
- Cordova - Device
- Cordova - Contacts
- Cordova - Camera
- Cordova - Battery Status
- Cordova - Plugman
- Cordova - Back Button
- Cordova - Events
- Cordova - Storage
- Cordova - Config.xml File
- Cordova - First Application
- Cordova - Environment Setup
- Cordova - Overview
- Cordova - Home
Cordova Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Cordova - Accelerometer
The Accelerometer plugin is also called the device-motion. It is used to track device motion in three dimensions.
Step 1 - Install Accelerometer Plugin
We will install this plugin by using cordova-CLI. Type the following code in the command prompt window.
C:UsersusernameDesktopCordovaProject>cordova plugin add cordova-plugindevice-motion
Step 2 - Add Buttons
In this step, we will add two buttons in the index.html file. One will be used for getting the current acceleration and the other will watch for the acceleration changes.
<button id = "getAcceleration">GET ACCELERATION</button> <button id = "watchAcceleration">WATCH ACCELERATION</button>
Step 3 - Add Event Listeners
Let us now add event psteners for our buttons to onDeviceReady function inside index.js.
document.getElementById("getAcceleration").addEventListener("cpck", getAcceleration); document.getElementById("watchAcceleration").addEventListener( "cpck", watchAcceleration);
Step 4 - Creating Functions
Now, we will create two functions. The first function will be used to get the current acceleration and the second function will watch the acceleration and the information about the acceleration will be triggered every three seconds. We will also add the clearWatch function wrapped by the setTimeout function to stop watching acceleration after the specified time frame. The frequency parameter is used to trigger the callback function every three seconds.
function getAcceleration() { navigator.accelerometer.getCurrentAcceleration( accelerometerSuccess, accelerometerError); function accelerometerSuccess(acceleration) { alert( Acceleration X: + acceleration.x + + Acceleration Y: + acceleration.y + + Acceleration Z: + acceleration.z + + Timestamp: + acceleration.timestamp + ); }; function accelerometerError() { alert( onError! ); }; } function watchAcceleration() { var accelerometerOptions = { frequency: 3000 } var watchID = navigator.accelerometer.watchAcceleration( accelerometerSuccess, accelerometerError, accelerometerOptions); function accelerometerSuccess(acceleration) { alert( Acceleration X: + acceleration.x + + Acceleration Y: + acceleration.y + + Acceleration Z: + acceleration.z + + Timestamp: + acceleration.timestamp + ); setTimeout(function() { navigator.accelerometer.clearWatch(watchID); }, 10000); }; function accelerometerError() { alert( onError! ); }; }
Now if we press the GET ACCELERATION button, we will get the current acceleration value. If we press the WATCH ACCELERATION button, the alert will be triggered every three seconds. After third alert is shown the clearWatch function will be called and we will not get any more alerts since we set timeout to 10000 milpseconds.
Advertisements