文档管理中心
指南运动健康服务应用接入端侧数据开放服务Android基础能力服务应用开发管理运动健康采样数据

管理运动健康采样数据

应用集成Health Service kit后,通过调用DataController类中的接口来操作用户的运动数据和健康数据, 如下:

在调用上述接口前,必须先初始化一个DataController对象如dataController,通过调用dataController对象的接口来操作用户数据。初始化DataController对象的示例代码如下:

// 请注意:
// 1.此处的this为Activity对象
// 2.此处的DataController类的对象,请在每次接口调用前初始化,避免相关Activity或Context失效导致的数据读取失败的问题
DataController dataController = HuaweiHiHealth.getDataController(this); // 此处的this为Activity对象
说明
  1. 在调用Health Service Kit SDK接口时,请确保手机处于亮屏状态,否则可能会导致接口调用失败,具体错误参见错误码:DISABLE_DATA_OPERATION。
  2. 在调用Health Service Kit SDK接口时,请确保您的应用为前台应用或者与前台应用绑定,否则可能会导致接口调用失败,具体错误请参见错误码:APPLICATION_NOT_FORGROUND。
  3. 在本开发指导中,使用步数增量(DT_CONTINUOUS_STEPS_DELTA)和身高(DT_INSTANTANEOUS_HEIGHT)两种数据类型进行示例说明,初始化DataController对象(dataController)时,仅请求了步数增量和身高的读写权限,即初始化后的dataController对象拥有步数增量和身高数据的读写权限。以下文档中context变量为android.content.Context对象,是Android系统提供的抽象实现类,具体代码中需依据具体场景获取对应的对象。
  4. 通过HuaweiHiHealth.getDataController获取DataController对象时,若传入Activity对象(用于应用包含界面的场景调用),支持触发HMS Core版本强制升级,若传入Context对象(用于应用无界面的场景调用),不支持触发HMS Core版本强制升级。
  5. 在开始调用Health Service Kit接口之前,请阅读Health Service Kit Android SDK接口调用示例章节,了解Health Service Kit Android SDK接口调用流程。

插入用户的运动健康数据

说明

请注意插入的运动健康数据的采样点数据的开始时间和结束时间均需大于2014年1月1号对应的UNIX时间戳。

  1. 构造一个数据采集器对象。
  2. 根据数据采集器,创建一个采样集。
  3. 构造一个步数增量(DT_CONTINUOUS_STEPS_DELTA)采样点的开始时间、结束时间、增量步数。
  4. 构造一个步数增量采样点。
  5. 将一个步数增量采样点存储到采样集,也可以重复执行步骤3 ~ 步骤5添加多个样本点数据到采样集。
  6. 调用insert接口, 将采样集插入到Health Service Kit中。
  7. 调用insert接口插入采样集数据是异步操作,需要设置成功或失败的Listener,用来返回数据插入成功或失败结果。

    插入用户的运动健康数据到Health Service Kit示例代码如下:
    // 步骤1:构造一个数据采集器对象。
    DataCollector dataCollector = new DataCollector.Builder().setPackageName(getApplicationContext())
        .setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
        .setDataStreamName("STEPS_DELTA")
        .setDataGenerateType(DataCollector.DATA_TYPE_RAW)
        .build();
    
    // 步骤2:根据数据采集器,创建一个采样集。
    final SampleSet sampleSet = SampleSet.create(dataCollector);
    
    // 步骤3:构造一个步数增量(DT_CONTINUOUS_STEPS_DELTA)采样点的开始时间、结束时间、增量步数。
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startDate = null;
    Date endDate = null;
    try {
        // 此处填写的开始、结束时间,存储时使用标准UNIX时间戳,时间戳不涉及时区
        // 请注意此处设置的插入运动健康数据的采样点数据的开始时间和结束时间均需大于2014年1月1号对应的UNIX时间戳,且时间间隔大于1分钟
        startDate = dateFormat.parse("2020-03-17 09:00:00");
        endDate = dateFormat.parse("2020-03-17 09:05:00");
    } catch (ParseException e) {
        logger("Time parsing error");
    }
    int stepsDelta = 200;
    
    // 步骤4:构造一个步数增量采样点。
    SamplePoint samplePoint = sampleSet.createSamplePoint()
        .setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS);
    samplePoint.getFieldValue(Field.FIELD_STEPS_DELTA).setIntValue(stepsDelta);
    // 若写入的步数数据需要在运动健康App首页上展示,需要使用addMetadata对采样点添加如下元数据
    samplePoint.addMetadata("motion_type", "RUN");
    
    // 步骤5:将一个步数增量采样点存储到采样集,也可以重复执行步骤3 ~ 步骤5添加多个样本点数据到采样集。
    sampleSet.addSample(samplePoint);
    
    // 步骤6:调用insert接口, 将采样集插入到Health Service Kit中。
    Task<Void> insertTask = dataController.insert(sampleSet);
    
    // 步骤7:调用insert接口插入采样集数据是异步操作,需要设置成功或失败的Listener,用来监听数据插入成功或失败。
    insertTask.addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void result) {
            logger("Successfully insert a SampleSet into HMS core");
            showSampleSet(sampleSet);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.i("DataController","Failed to insert the sample set into the HMS core:" + e.getMessage());
        }
    });
    说明

    上面的代码片段中用到两个辅助函数:Health Service Kit返回数据的显示函数showSampleSet(SampleSet sampleSet) ,输出结果保存到日志函数logger(String string)。这两个函数的代码示例如下:

    /**
     * 将SampleSet对象中的SamplePoint打印输出
     *
     * @param sampleSet 采样集
     */
    private void showSampleSet(SampleSet sampleSet) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        for (SamplePoint samplePoint : sampleSet.getSamplePoints()) {
            logger("Sample point type: " + samplePoint.getDataType().getName());
            logger("Start: " + dateFormat.format(new Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))));
            logger("End: " + dateFormat.format(new Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))));
            for (Field field : samplePoint.getDataType().getFields()) {
                logger("Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field));
            }
        }
    }
    
    /**
     * 输出操作结果日志到logcat
     *
     * @param string 日志字符串
     */
    private void logger(String string) {
        Log.i("DataController", string);
    }

删除用户的运动健康数据

从Health Service Kit删除历史数据,只能删除本应用插入的数据。

  1. 构造删除条件:一个数据采集器对象。
  2. 构造删除条件时间区间:开始、结束时间。
  3. 构造删除的参数对象。
  4. 使用指定的条件删除对象,调用delete接口删除数据样本集。
  5. 调用delete接口删除采样集数据是异步操作,需要设置成功或失败的Listener,用来监听数据删除成功或失败。

    // 步骤1:构造删除条件:一个数据采集器对象。
    DataCollector dataCollector = new DataCollector.Builder().setPackageName(getApplicationContext())
        .setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
        .setDataStreamName("STEPS_DELTA")
        .setDataGenerateType(DataCollector.DATA_TYPE_RAW)
        .build();
    
    // 步骤2:构造删除条件时间区间:开始、结束时间。
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startDate = null;
    Date endDate = null;
    try {
        // 此处填写的开始、结束时间,存储时使用标准UNIX时间戳。删除满足时间戳范围的数据点,时间戳不涉及时区
        startDate = dateFormat.parse("2020-03-17 09:00:00");
        endDate = dateFormat.parse("2020-03-17 09:05:00");
    } catch (ParseException e) {
        logger("Time parsing error");
    }
    
    // 步骤3:构造删除的参数对象。
    DeleteOptions deleteOptions = new DeleteOptions.Builder().addDataCollector(dataCollector)
        .setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS)
        .build();
    
    // 步骤4:使用指定的条件删除对象,调用delete接口删除数据样本集。
    Task<Void> deleteTask = dataController.delete(deleteOptions);
    
    // 步骤5:调用delete接口删除采样集数据是异步操作,需要设置成功或失败的Listener,用来监听数据删除成功或失败。
    deleteTask.addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void result) {
            logger("Successfully delete sample data from HMS core");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.i("DataController","Failed to delete sample data from HMS core:" + e.getMessage());
        }
    });
    说明

    上面的代码片段中用到辅助函数:输出结果保存到日志logger(String string)。这个函数的代码示例如下:

    /**
     * 输出操作结果日志到logcat
     *
     * @param string 日志字符串
     */
    private void logger(String string) {
        Log.i("DataController", string);
    }

修改用户的运动健康数据

说明

请注意修改的运动健康数据的区间范围的开始时间和结束时间均需大于2014年1月1号对应的UNIX时间戳。

  1. 构造修改条件:一个数据采集器对象。
  2. 构造修改采样集:根据数据采集器,创建一个修改采样集。
  3. 构造一个修改步数增量(DT_CONTINUOUS_STEPS_DELTA)采样点的开始,结束时间,增量步数。
  4. 构造一个修改步数增量采样点。
  5. 将一个修改步数增量采样点添加到修改采样集,也可以重复执行步骤3 ~ 步骤5添加多个修改样本点数据到修改采样集。
  6. 构造一个修改操作的参数对象。
  7. 使用指定的修改参数对象,调用update接口修改数据样本集。
  8. 调用update接口修改采样集数据是异步操作,需要设置成功或失败的Listener,用来返回数据修改成功或失败。

    // 步骤1:构造修改条件:一个数据采集器对象。
    DataCollector dataCollector = new DataCollector.Builder().setPackageName(getApplicationContext())
        .setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
        .setDataStreamName("STEPS_DELTA")
        .setDataGenerateType(DataCollector.DATA_TYPE_RAW)
        .build();
    
    // 步骤2:构造修改采样集:根据数据采集器,创建一个修改采样集。
    SampleSet sampleSet = SampleSet.create(dataCollector);
    
    // 步骤3:构造一个修改步数增量(DT_CONTINUOUS_STEPS_DELTA)采样点的开始,结束时间,增量步数。
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startDate = null;
    Date endDate = null;
    try {
        // 此处填写的开始、结束时间,存储时使用标准UNIX时间戳。更新满足时间戳范围的数据点,时间戳不涉及时区
        // 请注意此处设置的修改运动健康数据的区间范围的开始时间和结束时间均需大于2014年1月1号对应的UNIX时间戳
        startDate = dateFormat.parse("2020-03-17 09:00:00");
        endDate = dateFormat.parse("2020-03-17 09:05:00");
    } catch (ParseException exception) {
        logger("Time parsing error");
    }
    int stepsDelta = 200;
    
    // 步骤4:构造一个修改步数增量采样点。
    SamplePoint samplePoint = sampleSet.createSamplePoint()
        .setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS);
    samplePoint.getFieldValue(Field.FIELD_STEPS_DELTA).setIntValue(stepsDelta);
    // 若修改的步数数据需要在运动健康App首页上展示,需要使用addMetadata对采样点添加如下元数据
    samplePoint.addMetadata("motion_type", "RUN");
    
    // 步骤5:将一个修改步数增量采样点添加到修改采样集,也可以重复执行步骤3 ~ 步骤5添加多个修改样本点数据到修改采样集。
    sampleSet.addSample(samplePoint);
    
    // 步骤6:构造一个修改操作的参数对象。
    UpdateOptions updateOption = new UpdateOptions.Builder()
        .setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS)
        .setSampleSet(sampleSet)
        .build();
    
    // 步骤7:使用指定的修改参数对象,调用update接口修改数据样本集。
    Task<Void> updateTask = dataController.update(updateOption);
    
    // 步骤8:调用update接口修改采样集数据是异步操作,需要设置成功或失败的Listener,用来返回数据修改成功或失败。
    updateTask.addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void result) {
            logger("Successfully update sample data from HMS core");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.i("DataController","Failed to update sample data from HMS core:" + e.getMessage());
        }
    });
    说明

    上面的代码片段中用到辅助函数:输出结果保存到日志函数logger(String string)。这个函数的代码示例如下:

    /**
     * 输出操作结果日志到logcat
     *
     * @param string 日志字符串
     */
    private void logger(String string) {
        Log.i("DataController", string);
    }

查询用户的运动健康数据

从Health Service Kit查询历史数据,例如读取某一时间段的步数,可以在ReadOptions指定读取的条件,如指定数据采集器、读取的数据类型、读取详情数据。

说明
  1. 调用查询运动健康数据接口,接口参数中的开始时间和结束时间均需大于2014年1月1号对应的UNIX时间戳。
  2. 调用查询运动健康数据接口,接口参数中的开始时间和结束时间之间的时间跨度不允许超过31天。
  3. 调用查询运动健康数据接口,查询步数详情数据为空,请参见FAQ:查询步数明细数据时,查询不到数据,如何处理。
  1. 构造查询条件时间区间:开始、结束时间。
  2. 构造查询条件的查询对象。
  3. 使用指定的条件查询对象,调用read接口查询数据集。
  4. 调用read接口查询采样集数据是异步操作,需要设置成功或失败的Listener,用来监听数据查询成功或失败。

    // 步骤1:构造查询条件时间区间:开始、结束时间。
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startDate = null;
    Date endDate = null;
    try {
        // 此处填写的开始、结束时间,存储时使用标准UNIX时间戳。查询时间戳范围的数据点,时间戳不涉及时区。
        // 请注意此处设置的查询运动健康数据的区间范围的开始时间和结束时间均需大于2014年1月1号对应的UNIX时间戳。
        startDate = dateFormat.parse("2020-03-17 09:00:00");
        endDate = dateFormat.parse("2020-03-17 09:05:00");
    } catch (ParseException exception) {
        logger("Time parsing error");
    }
    
    // 步骤2:构造查询条件的查询对象。
    // 此处填写的DataType.DT_CONTINUOUS_STEPS_DELTA,可用来查询用户的步数健康数据。
    ReadOptions readOptions = new ReadOptions.Builder().read(DataType.DT_CONTINUOUS_STEPS_DELTA)
        .setTimeRange(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS)
        .build();
    
    // 步骤3:使用指定的条件查询对象,调用read接口查询数据集。
    Task<ReadReply> readReplyTask = dataController.read(readOptions);
    
    // 步骤4:调用read接口查询采样集数据是异步操作,需要设置成功或失败的Listener,用来监听数据查询成功或失败。
    readReplyTask.addOnSuccessListener(new OnSuccessListener<ReadReply>() {
        @Override
        public void onSuccess(ReadReply readReply) {
            logger("Successfully read a SampleSet from HMS core");
            for (SampleSet sampleSet : readReply.getSampleSets()) {
                showSampleSet(sampleSet);
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.i("DataController","Failed to read a SampleSet from HMS core:" + e.getMessage());
        }
    });
    说明

    上面的代码片段中用到两个辅助函数:Health Service Kit返回数据的显示函数showSampleSet(SampleSet sampleSet) ,输出结果保存到日志函数logger(String string)。这两个函数的代码示例如下:

    /**
     * 将SampleSet对象中的SamplePoint打印输出
     *
     * @param sampleSet 采样集
     */
    private void showSampleSet(SampleSet sampleSet) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        for (SamplePoint samplePoint : sampleSet.getSamplePoints()) {
            logger("Sample point type: " + samplePoint.getDataType().getName());
            logger("Start: " + dateFormat.format(new Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))));
            logger("End: " + dateFormat.format(new Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))));
            for (Field field : samplePoint.getDataType().getFields()) {
                logger("Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field));
            }
        }
    }
    
    /**
     * 输出操作结果日志到logcat
     *
     * @param string 日志字符串
     */
    private void logger(String string) {
        Log.i("DataController", string);
    }

查询用户的运动健康数据当日统计值

说明

查询用户运动健康数据的当日统计值接口,支持的数据类型参见readTodaySummation的入参说明。

  1. 使用指定的数据类型(步数增量:DT_CONTINUOUS_STEPS_DELTA),调用readTodaySummation接口查询该数据类型的当日统计值。
  2. 调用readTodaySummation接口查询当日的统计值是异步操作,需要设置成功或失败的Listener,用来监听数据查询成功或失败。

    说明

    该接口查询的是某种数据类型的当日统计值,查询时间范围为当日00:00:00的时间戳至接口调用时当前系统时间戳。该接口将开始时间,或者结束时间落在该查询时间范围内的所有数据点进行统计计算,返回一个统计结果,当前只支持上面说明中的数据类型。

    // 步骤1:使用指定的数据类型(步数增量:DT_CONTINUOUS_STEPS_DELTA),调用readTodaySummation接口查询该数据类型的当日统计值。
    Task<SampleSet> todaySummationTask = dataController.readTodaySummation(DataType.DT_CONTINUOUS_STEPS_DELTA);
    
    // 步骤2:调用readTodaySummation接口查询当日的统计值是异步操作,需要设置成功或失败的Listener,用来监听数据查询成功或失败。
    todaySummationTask.addOnSuccessListener(new OnSuccessListener<SampleSet>() {
        @Override
        public void onSuccess(SampleSet sampleSet) {
            logger("Successfully read today summation from HMS core");
            if (sampleSet != null) {
                showSampleSet(sampleSet);
            }
        }
    });
    todaySummationTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.i("DataController","Failed to read today summation from HMS core:" + e.getMessage());
        }
    });
    说明

    上面的代码片段中用到两个辅助函数:Health Service Kit返回数据的显示函数showSampleSet(SampleSet sampleSet) ,输出结果保存到日志函数logger(String string)。这两个函数的代码示例如下:

    /**
     * 将SampleSet对象中的SamplePoint打印输出
     *
     * @param sampleSet 采样集
     */
    private void showSampleSet(SampleSet sampleSet) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        for (SamplePoint samplePoint : sampleSet.getSamplePoints()) {
            logger("Sample point type: " + samplePoint.getDataType().getName());
            logger("Start: " + dateFormat.format(new Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))));
            logger("End: " + dateFormat.format(new Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))));
            for (Field field : samplePoint.getDataType().getFields()) {
                logger("Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field));
            }
        }
    }
    
    /**
     * 输出操作结果日志到logcat
     *
     * @param string 日志字符串
     */
    private void logger(String string) {
        Log.i("DataController", string);
    }

查询用户的运动健康数据多日统计值

该接口查询的是某种数据类型的多天的日统计值,查询时间范围为入参传入的开始时间和结束时间(以天为单位)。

将查询到的开始时间或者结束时间落在该查询时间范围内的所有数据点进行按天分组,然后对每一组(以天为单位)的数据进行统计计算,返回一个多天的日统计值列表。

说明
  1. 调用查询运动健康数据多日统计值接口,接口支持的数据类型参见readDailySummation的入参说明。
  2. 调用查询运动健康数据多日统计值接口,接口参数中的开始时间和结束时间均需大于20140101代表的2014年1月1号的日期时间。
  3. 调用查询运动健康数据多日统计值接口,接口参数中的开始时间和结束时间之间的时间跨度不允许超过31天。
  1. 初始化开始和结束时间,整型数据的前四位代表年份,中间两位代表月份,最后两位代表日期。
  2. 调用readDailySummation接口查询指定数据类型(步数增量)在起止时间内的多日统计值。
  3. 调用readDailySummation接口查询多天的日统计值是异步操作,需要设置成功或失败的Listener,用来返回数据查询成功或失败。

    // 步骤1:初始化开始和结束时间,整型数据的前四位代表年份,中间两位代表月份,最后两位代表日期。
    // 请注意此处设置的开始时间和结束时间均需大于20140101代表的2014年1月1号的日期时间。
    int startTime = 20200818;
    int endTime = 20200825;
    
    // 步骤2:调用readDailySummation接口查询指定数据类型(步数增量)在起止时间内的多日统计值。
    Task<SampleSet> daliySummationTask = dataController.readDailySummation(DataType.DT_CONTINUOUS_STEPS_DELTA, startTime, endTime);
    
    // 步骤3:调用readDailySummation接口查询多天的日统计值是异步操作,需要设置成功或失败的Listener,用来返回数据查询成功或失败。
    daliySummationTask.addOnSuccessListener(new OnSuccessListener<SampleSet>() {
        @Override
        public void onSuccess(SampleSet sampleSet) {
            logger("Successfully read daily summation from HMS core");
            if (sampleSet != null) {
                showSampleSet(sampleSet);
            }
        }
    });
    daliySummationTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            logger("readDailySummation" + e.toString());
        }
    });
    说明

    上面的代码片段中用到两个辅助函数:Health Service Kit返回数据的显示函数showSampleSet(SampleSet sampleSet) ,输出结果保存到日志函数logger(String string)。这两个函数的代码示例如下:

    /**
     * 将SampleSet对象中的SamplePoint打印输出
     *
     * @param sampleSet 采样集
     */
    private void showSampleSet(SampleSet sampleSet) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        for (SamplePoint samplePoint : sampleSet.getSamplePoints()) {
            logger("Sample point type: " + samplePoint.getDataType().getName());
            logger("Start: " + dateFormat.format(new Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))));
            logger("End: " + dateFormat.format(new Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))));
            for (Field field : samplePoint.getDataType().getFields()) {
                logger("Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field));
            }
        }
    }
    
    /**
     * 输出操作结果日志到logcat
     *
     * @param string 日志字符串
     */
    private void logger(String string) {
        Log.i("DataController", string);
    }

查询用户的运动健康数据最新数据点

说明

查询用户的运动健康数据最新数据点接口,支持的数据类型参见readLatestData的入参说明。

  1. 使用指定的数据类型(身高:DT_INSTANTANEOUS_HEIGHT),调用readLatestData接口查询该数据类型的最新数据点。
  2. 调用readLatestData接口查询最新数据点是异步操作,需要设置成功或失败的Listener,用来返回数据查询成功或失败。

    // 步骤1:使用指定的数据类型(身高:DT_INSTANTANEOUS_HEIGHT),调用readLatestData接口查询该数据类型的最新数据点。
    List<DataType> dataTypes = new ArrayList<>();
    dataTypes.add(DataType.DT_INSTANTANEOUS_HEIGHT);
    Task<Map<DataType, SamplePoint>> readLatestDatas = dataController.readLatestData(dataTypes);
    
    // 步骤2:调用readLatestData接口查询最新数据点是异步操作,需要设置成功或失败的Listener,用来返回数据查询成功或失败。
    readLatestDatas.addOnSuccessListener(new OnSuccessListener<Map<DataType, SamplePoint>>() {
        @Override
        public void onSuccess(Map<DataType, SamplePoint> samplePointMap) {
            logger("Success read latest data from HMS core");
            if (samplePointMap != null) {
                for (DataType dataType : dataTypes) {
                    if (samplePointMap.containsKey(dataType)) {
                        showSamplePoint(samplePointMap.get(dataType));
                    } else {
                        logger("The DataType " + dataType.getName() + " has no latest data");
                    }
                }
            }
        }
    });
    readLatestDatas.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.i("DataController","Failed to read latest data from HMS core:" + e.getMessage());
        }
    });
    说明

    上面的代码片段中用到两个辅助函数:Health Service Kit返回数据的显示函数showSamplePoint(SamplePoint samplePoint),输出结果保存到日志函数logger(String string)。这两个函数的代码示例如下:

    /**
     * 将SamplePoint打印输出
     *
     * @param samplePoint 采样集
     */
    private void showSamplePoint(SamplePoint samplePoint) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        logger("Sample point type: " + samplePoint.getDataType().getName());
        logger("Start: " + dateFormat.format(new Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))));
        logger("End: " + dateFormat.format(new Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))));
        for (Field field : samplePoint.getDataType().getFields()) {
            logger("Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field));
        }
        logger(System.lineSeparator());
    }
    
    /**
     * 输出操作结果日志到logcat
     *
     * @param string 日志字符串
     */
    private void logger(String string) {
        Log.i("DataController", string);
    }

清空用户端云两侧的运动与健康数据

  1. 调用clearAll接口:清空端云所有本应用插入的数据。
  2. 调用clearAll接口清空端云所有的用户数据是异步操作,需要设置成功或失败Listener,用来返回清空数据成功或失败。

    // 步骤1:调用clearAll接口:清空端云所有本应用插入的数据。
    Task<Void> clearAllTask = dataController.clearAll();
    
    // 步骤2:调用clearAll接口清空端云所有的用户数据是异步操作,需要设置成功或失败Listener,用来返回清空数据成功或失败。
    clearAllTask.addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void result) {
            logger("clearAll success");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.i("DataController","Failed to clear data:" + e.getMessage());
        }
    });
    说明

    上面的代码片段中用到辅助函数:输出结果保存到日志及显示到页面TextView控件函数logger(String string)。这个函数的代码示例如下:

    /**
     * 同时输出操作结果日志到logcat和页面上的TextView控件
     *
     * @param string 日志字符串
     */
    private void logger(String string) {
        Log.i("DataController", string);
    }

在 指南 中进行搜索
请输入您想要搜索的关键词