读取锻炼记录
场景介绍
读取最新一条锻炼记录。
约束与限制
从5.1.1(19) Release版本开始支持。
接口说明
| 接口名 | 描述 |
|---|---|
| readData<T extends ExerciseSequence>(request: ExerciseSequenceReadRequest): Promise<T[]> | 查询最新一条锻炼记录。 |
当前ExerciseSequenceReadRequest里的时间参数暂不生效,仅支持返回手表侧最新一条数据。
开发前检查
- 完成申请运动健康服务与配置Client ID。
- 接口首次调用前,需先使用init方法进行初始化。
- 需先通过用户授权接口引导用户授权,用户授权对应数据类型权限后,才有权限调用接口操作相关数据类型数据。
- 错误码请参考ArkTS API错误码,常见问题请参考Health Service Kit常见问题。
开发步骤
-
导入运动健康服务功能模块及相关公共模块。
import { healthStore } from '@kit.HealthServiceKit';import { hilog } from '@kit.PerformanceAnalysisKit'; -
创建查询请求。
// 查询跑步记录const sequenceReadRequest: healthStore.ExerciseSequenceReadRequest<healthStore.exerciseSequenceHelper.running.DetailFields> = {startTime: 1698040800000,endTime: 1698042600000,exerciseType: healthStore.exerciseSequenceHelper.running.EXERCISE_TYPE,count: 1,sortOrder: 1,readOptions: {withPartialDetails: ['exerciseHeartRate', 'altitude']}}; -
调用readData方法执行查询请求,并处理返回结果。
try {const runningSequences = await healthStore.readData<healthStore.exerciseSequenceHelper.running.Model>(sequenceReadRequest);hilog.info(0x0000, 'testTag', 'Succeeded in reading data.');runningSequences.forEach((runningSequence) => {hilog.info(0x0000, 'testTag', `the start time is ${runningSequence.startTime}.`);hilog.info(0x0000, 'testTag', `the end time is ${runningSequence.endTime}.`);Object.keys(runningSequence.summaries).forEach((key) => {Object.keys(runningSequence.summaries[key]).forEach((fieldName) => {hilog.info(0x0000, 'testTag', `the summaries of ${key} field ${fieldName} is ${runningSequence.summaries[key][fieldName]}.`);});});});} catch (err) {hilog.error(0x0000, 'testTag', `Failed to read data. Code: ${err.code}, message: ${err.message}`);}