has.startAccelerometer(Object object)
Starts listening for acceleration data.
Since: 1.0.4
Required permission: ohos.permission.ACCELEROMETER, which needs to be declared in module.json5
Parameters
The input parameter is an object containing the following fields.
Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
interval | string | normal | No | Execution frequency for the callback of device orientation changes.
|
success | function | - | No | Callback to be invoked when the API call is successful. |
fail | function | - | No | Callback to be invoked when the API call fails. |
complete | function | - | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
has.startAccelerometer({
success() {
console.log('startAccelerometer success: ');
},
fail(res) {
console.log('startAccelerometer fail: ', JSON.stringify(res));
},
complete() {
console.log('startAccelerometer complete: ');
}
}); has.stopAccelerometer(Object object)
Stops listening for acceleration data.
Since: 1.0.4
Required permission: ohos.permission.ACCELEROMETER, which needs to be declared in module.json5
Parameters
The input parameter is an object containing the following fields.
Name | Type | Mandatory | Description |
|---|---|---|---|
success | function | No | Callback to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
has.stopAccelerometer({
success() {
console.log('stopAccelerometer success: ');
},
fail(res) {
console.log('stopAccelerometer fail: ', JSON.stringify(res));
},
complete() {
console.log('stopAccelerometer complete: ');
}
}); has.onAccelerometerChange(function callback)
Enables listening for acceleration change events.
Since: 1.0.4
Parameters
Name | Type | Mandatory | Description |
|---|---|---|---|
callback | function | No | Callback for the acceleration change event. |
Return value of callback
Name | Type | Description |
|---|---|---|
x | number | Acceleration along the x-axis of the device, in m/s². |
y | number | Acceleration along the y-axis of the device, in m/s². |
z | number | Acceleration along the z-axis of the device, in m/s². |
has.onAccelerometerChange(function (res) {
console.log('onAccelerometerChange');
}); has.offAccelerometerChange(function callback)
Cancels listening for acceleration change events. If the callback parameter is left empty, listening will be disabled for all events.
Since: 1.0.4
Parameters
Name | Type | Mandatory | Description |
|---|---|---|---|
callback | function | No | Callback for the operation result. |
const fun = function (res) {
console.log('onAccelerometerChange');
};
has.onAccelerometerChange(fun);
has.offAccelerometerChange(fun);