文档管理中心
ASCF框架API参考API设备加速计

加速计

has.startAccelerometer

has.startAccelerometer(Object object)

开始监听加速度数据。

起始版本: 1.0.4

需要权限:在module.json5文件中声明ohos.permission.ACCELEROMETER

参数:

参数为Object对象,包括以下字段。

展开
参数 类型 默认值 必填 描述
interval string normal

监听加速度数据回调函数的执行频率。

- game:适用于更新游戏的回调频率,在20ms/次 左右。

- ui:适用于更新UI的回调频率,在60ms/次 左右。

- normal:普通的回调频率,在200ms/次 左右。

success function - 接口调用成功的回调函数。
fail function - 接口调用失败的回调函数。
complete function - 接口调用结束的回调函数(调用成功、失败都会执行)。

示例:

收起
自动换行
深色代码主题
复制
  1. has.startAccelerometer({
  2. success: () => {
  3. console.info('startAccelerometer success');
  4. },
  5. fail: (err) => {
  6. console.error('startAccelerometer fail', err);
  7. },
  8. complete: (res) => {
  9. console.info('startAccelerometer complete', res);
  10. }
  11. });

has.stopAccelerometer

has.stopAccelerometer(Object object)

停止监听加速度数据。

起始版本: 1.0.4

需要权限:在module.json5文件中声明ohos.permission.ACCELEROMETER

参数:

参数为Object对象,包括以下字段。

展开
参数 类型 必填 描述
success function 接口调用成功的回调函数。
fail function 接口调用失败的回调函数。
complete function 接口调用结束的回调函数(调用成功、失败都会执行)。

示例:

收起
自动换行
深色代码主题
复制
  1. has.stopAccelerometer({
  2. success: () => {
  3. console.info('stopAccelerometer success');
  4. },
  5. fail: (err) => {
  6. console.error('stopAccelerometer fail', err);
  7. },
  8. complete: (res) => {
  9. console.info('stopAccelerometer complete', res);
  10. }
  11. });

has.onAccelerometerChange

has.onAccelerometerChange(function callback)

监听加速度数据变化事件。

起始版本: 1.0.4

参数:

展开
参数 类型 必填 描述
callback function 加速度变化事件的回调函数。

callback返回值:

展开
参数 类型 描述
x number 施加在设备x轴的加速度,单位:m/s²。
y number 施加在设备y轴的加速度,单位:m/s²。
z number 施加在设备z轴的加速度,单位:m/s²。

示例:

收起
自动换行
深色代码主题
复制
  1. has.onAccelerometerChange(function(res) {
  2. console.info('onAccelerometerChange callback triggered', res);
  3. });

has.offAccelerometerChange

has.offAccelerometerChange(function callback)

移除加速度数据变化事件的监听函数。

起始版本: 1.0.4

参数:

展开
参数 类型 必填 描述
callback function has.onAccelerometerChange传入的监听函数。不传此参数则移除所有监听函数。

示例:

收起
自动换行
深色代码主题
复制
  1. const onAccelerometerChangeFn = function(res) {
  2. console.info('onAccelerometerChange callback triggered', res);
  3. };
  4. has.onAccelerometerChange(onAccelerometerChangeFn);
  5. has.offAccelerometerChange(onAccelerometerChangeFn);
相关推荐
在 ASCF框架 中进行搜索
请输入您想要搜索的关键词