# 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

# 参数

名称 类型 必填 默认值 兼容性 描述
callback (result: OnAccelerometerChangeCallbackResult) => void -
-
加速度数据事件的监听函数

# 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

# 参数

名称 类型 必填 默认值 兼容性 描述
callback (result: OnAccelerometerChangeCallbackResult) => void -
-
onAccelerometerChange 传入的监听函数。不传此参数则移除所有监听函数。

# 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 -
-
开始监听加速度数据的参数
名称 类型 必备 默认值 兼容性 描述
interval string -
-
监听加速度数据回调函数的执行频率
合法值 兼容性 描述
game
-
适用于更新游戏的回调频率,约 20ms/次 左右
ui
-
适用于更新 UI 的回调频率,约 60ms/次 左右
normal
-
普通的回调频率,约 200ms/次 左右
success (res: StartAccelerometerSuccess) => void -
-
监听加速度数据调用成功的回调函数
fail (res: StartAccelerometerFail) => void -
-
监听加速度数据调用失败的回调函数
complete (res: StartAccelerometerSuccess | StartAccelerometerFail) => void -
-
监听加速度数据调用结束的回调函数(调用成功、失败都会执行)

# 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 -
-
停止监听加速度数据的参数
名称 类型 必备 默认值 兼容性 描述
success (res: StopAccelerometerSuccess) => void -
-
停止监听加速度数据调用成功的回调函数
fail (res: StopAccelerometerFail) => void -
-
停止监听加速度数据调用失败的回调函数
complete (res: StopAccelerometerSuccess | StopAccelerometerFail) => void -
-
停止监听加速度数据调用结束的回调函数(调用成功、失败都会执行)

# 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分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<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+ 版本中将和微信小程序行为保持一致。