uni.onAccelerometerChange(callback)
监听加速度数据变化事件
onAccelerometerChange 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
| 4.0 | 4.41 | 5.08 | 5.08 | x | 5.08 | 5.08 |
参数
OnAccelerometerChangeCallbackResult 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| x | number | 是 | | | X 轴加速度数据 |
| y | number | 是 | | | Y 轴加速度数据 |
| z | number | 是 | | | Z 轴加速度数据 |
参见
注意
- 鸿蒙平台权限设置 此接口需要如下权限:具体配置方法请参考鸿蒙权限配置指南
ohos.permission.ACCELEROMETER: 注册监听时如果当前未启动,会自动调用 startAccelerometer(),因此间接触发权限申请。
uni.offAccelerometerChange(callback)
监听加速度数据变化事件
offAccelerometerChange 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
| 4.0 | 4.41 | 5.08 | 5.08 | x | 5.08 | 5.08 |
参数
OnAccelerometerChangeCallbackResult 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| x | number | 是 | | | X 轴加速度数据 |
| y | number | 是 | | | Y 轴加速度数据 |
| z | number | 是 | | | Z 轴加速度数据 |
参见
uni.startAccelerometer(options)
开始监听加速度数据变化事件
startAccelerometer 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
| 4.0 | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| options | StartAccelerometerOptions | 否 | | | 开始监听加速度数据的参数 |
|
StartAccelerometerSuccess 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 否 | | | |
StartAccelerometerFail 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errCode | number | 是 | | | |
| 合法值 | 描述 | | 501 | | | 502 | | | 503 | | | 601 | | | 602 | | | 603 | | | 604 | | | 701 | | | 702 | | | 703 | | | 801 | | | 802 | | | 803 | | | 804 | | | 901 | |
|
| errSubject | string | 是 | | | 统一错误主题(模块)名称 |
| data | any | 否 | | | 错误信息中包含的数据 |
| cause | Error | 否 | | | 源错误信息,可以包含多个错误,详见SourceError |
| errMsg | string | 是 | | | |
参见
注意
- 鸿蒙平台权限设置 此接口需要权限
ohos.permission.ACCELEROMETER。具体配置方法请参考鸿蒙权限配置指南
uni.stopAccelerometer(options)
停止监听加速度数据变化事件
stopAccelerometer 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
| 4.0 | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| options | StopAccelerometerOptions | 否 | | | 停止监听加速度数据的参数 |
|
StopAccelerometerSuccess 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 否 | | | |
StopAccelerometerFail 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errCode | number | 是 | | | |
| 合法值 | 描述 | | 501 | | | 502 | | | 503 | | | 601 | | | 602 | | | 603 | | | 604 | | | 701 | | | 702 | | | 703 | | | 801 | | | 802 | | | 803 | | | 804 | | | 901 | |
|
| errSubject | string | 是 | | | 统一错误主题(模块)名称 |
| data | any | 否 | | | 错误信息中包含的数据 |
| cause | Error | 否 | | | 源错误信息,可以包含多个错误,详见SourceError |
| errMsg | string | 是 | | | |
参见
示例
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
扫码体验 示例源码如下,请查看 pre > code 标签中的内容
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1;padding: 6px;">
<!-- #endif -->
<text class="uni-h2">加速度计</text>
<text>开始监听后可实时查看设备在 X、Y、Z 轴上的加速度数据。</text>
<text class="margin-v">当前状态:{{ statusText }}</text>
<text class="uni-h3">采样频率</text>
<radio-group class="option-group" @change="handleIntervalChange">
<view class="option-item" v-for="item in intervalOptions" :key="item.value">
<radio :value="item.value" :checked="activeInterval == item.value">
<text class="option-text">{{ item.label }}</text>
</radio>
</view>
</radio-group>
<button class="margin-v" type="primary" @tap="startListen">开始监听</button>
<button class="margin-v" @tap="stopListen">停止监听</button>
<button class="margin-v" @tap="navigateToShake">跳转摇一摇</button>
<text class="uni-h3">实时数据</text>
<view class="log-list">
<text class="log-item" v-for="item in axisItems" :key="item.key">{{ item.key }}:{{ item.value }}</text>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
type PageAccelerometerInterval = 'game' | 'ui' | 'normal'
type IntervalOption = {
value : PageAccelerometerInterval
label : string
}
type AxisItem = {
key : string
value : string
}
const intervalOptions : IntervalOption[] = [
{ value: 'normal', label: 'normal' },
{ value: 'ui', label: 'ui' },
{ value: 'game', label: 'game' }
]
const activeInterval = ref<PageAccelerometerInterval>('normal')
const statusText = ref('未启动')
const axisX = ref('0.000')
const axisY = ref('0.000')
const axisZ = ref('0.000')
const axisItems = computed((): AxisItem[] => {
return [
{ key: 'X', value: axisX.value },
{ key: 'Y', value: axisY.value },
{ key: 'Z', value: axisZ.value }
]
})
function formatAxisValue(value : number) : string {
return value.toFixed(3)
}
const accelerometerListener : OnAccelerometerChangeCallback = (result : OnAccelerometerChangeCallbackResult) => {
axisX.value = formatAxisValue(result.x)
axisY.value = formatAxisValue(result.y)
axisZ.value = formatAxisValue(result.z)
}
function handleIntervalChange(event : UniRadioGroupChangeEvent) : void {
activeInterval.value = event.detail.value as PageAccelerometerInterval
}
function startListen() : void {
uni.onAccelerometerChange(accelerometerListener)
uni.startAccelerometer({
interval: activeInterval.value,
success: () => {
statusText.value = `监听中 (${activeInterval.value})`
},
fail: (error) => {
statusText.value = error.errMsg != null ? error.errMsg : '启动失败'
console.error('startAccelerometer:fail', error)
}
})
}
function stopListen() : void {
uni.stopAccelerometer({
success: () => {
statusText.value = '已停止'
}
})
}
function navigateToShake() : void {
uni.navigateTo({
url: '/pages/API/accelerometer/shake'
})
}
onUnload(() => {
uni.offAccelerometerChange(accelerometerListener)
uni.stopAccelerometer()
})
</script>
<style>
.margin-v {
margin: 5px 0;
}
.option-group {
margin: 5px 0;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.option-item {
display: flex;
flex-direction: row;
align-items: center;
margin-right: 18px;
margin-top: 8px;
}
.option-text {
font-size: 14px;
}
.log-list {
margin: 5px 0;
padding: 10px;
border: 1px solid #ccc;
display: flex;
flex-direction: column;
}
.log-item {
font-size: 14px;
margin: 4px 0;
}
</style>
通用类型
GeneralCallbackResult
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 是 | | | 错误信息 |
tips
- 和微信小程序平台不同,app 平台调用
uni.onAccelerometerChange 时不会自动启动加速器,需要调用 uni.startAccelerometer() 才能正常启动加速器。这个问题会在后续版本处理,app 平台在 5.09+ 版本中将和微信小程序行为保持一致。