监听加速度数据变化事件
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| 4.0 | 4.41 | 5.08 | 5.08 | x | 5.08 | 5.08 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| callback | (result: OnAccelerometerChangeCallbackResult) => void | 是 | - | - | 加速度数据事件的监听函数 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| x | number | 是 | - | - | X 轴加速度数据 |
| y | number | 是 | - | - | Y 轴加速度数据 |
| z | number | 是 | - | - | Z 轴加速度数据 |
ohos.permission.ACCELEROMETER: 注册监听时如果当前未启动,会自动调用 startAccelerometer(),因此间接触发权限申请。
监听加速度数据变化事件
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|---|
| 4.0 | 4.41 | 5.08 | 5.08 | x | 5.08 | 5.08 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| callback | (result: OnAccelerometerChangeCallbackResult) => void | 否 | - | - | onAccelerometerChange 传入的监听函数。不传此参数则移除所有监听函数。 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| x | number | 是 | - | - | X 轴加速度数据 |
| y | number | 是 | - | - | Y 轴加速度数据 |
| z | number | 是 | - | - | Z 轴加速度数据 |
开始监听加速度数据变化事件
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | StartAccelerometerOptions | 否 | - | - | 开始监听加速度数据的参数 | ||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 否 | - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| errSubject | string | 是 | - | - | 统一错误主题(模块)名称 | ||||||||||||||||||||||||||||||||||||||||||||||||
| data | any | 否 | - | - | 错误信息中包含的数据 | ||||||||||||||||||||||||||||||||||||||||||||||||
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | ||||||||||||||||||||||||||||||||||||||||||||||||
| errMsg | string | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||
ohos.permission.ACCELEROMETER。具体配置方法请参考鸿蒙权限配置指南停止监听加速度数据变化事件
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | StopAccelerometerOptions | 否 | - | - | 停止监听加速度数据的参数 | ||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 否 | - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| errSubject | string | 是 | - | - | 统一错误主题(模块)名称 | ||||||||||||||||||||||||||||||||||||||||||||||||
| data | any | 否 | - | - | 错误信息中包含的数据 | ||||||||||||||||||||||||||||||||||||||||||||||||
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | ||||||||||||||||||||||||||||||||||||||||||||||||
| errMsg | string | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例
<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>
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | 错误信息 |
uni.onAccelerometerChange 时不会自动启动加速器,需要调用 uni.startAccelerometer() 才能正常启动加速器。这个问题会在后续版本处理,app 平台在 5.09+ 版本中将和微信小程序行为保持一致。