After version 3.0, Cordova implements the device API via plug-in mode, and the CLI's plugin command allows you to add or remove plugins:
$ cordova plugin add org.apache.cordova.device-motion
$ cordova plugin ls
[ ‘org.apache.cordova.device-motion‘ ]
$ cordova plugin rm org.apache.cordova.device-motion
This command can be applied to all platforms, but modifying platform-specific configuration settings requires the use of the following methods
-
- Amazon Fire OS (in Res/xml/config.xml)
<feature name="Accelerometer">
<param name="android-package" value="org.apache.cordova.devicemotion.AccelListener" />
</feature>
-
- Android (in Res/xml/config.xml)
<feature name="Accelerometer">
<param name="android-package" value="org.apache.cordova.devicemotion.AccelListener" />
</feature>
-
- BlackBerry 10 (in Www/config.xml)
<feature name= "Accelerometer" value= "Accelerometer"/>
-
- IOS (in the application name of the config file)
<feature name="Accelerometer">
<param name="ios-package" value="CDVAccelerometer" />
</feature>
-
- Windows Phone (in Properties/wpappmanifest.xml)
<Capabilities>
<Capability Name="ID_CAP_SENSORS" />
</Capabilities>
functionAccelerometer.getcurrentaccelerometer ()gets the acceleration in the leading X, y, and z directions.
Navigator.accelerometer.getCurrentAcceleration (accelerometersuccess, accelerometererror);
DescriptionThe accelerometer is a motion sensor that detects changes in the x/y/z direction of the device in three-dimensional space relative to the previous moment. These values are returned by the arguments passed to accelerometersuccess back to the function. Simple example
function onSuccess (acceleration) {
alert (‘Acceleration X:‘ + acceleration.x + ‘\ n’ +
‘Acceleration Y:‘ + acceleration.y + ‘\ n’ +
‘Acceleration Z:‘ + acceleration.z + ‘\ n’ +
‘Timestamp:‘ + acceleration.timestamp + ‘\ n’);
};
function onError () {
alert (‘onError!’);
};
document.addEventListener ("deviceready", onDeviceReady, false);
function onDeviceReady () {
navigator.accelerometer.getCurrentAcceleration (onSuccess, onError);
}
For remote use, pay attention to the need to copy plugins and so on.
Cordova,phonegap Gravity sensor