应用集成Health Service kit后,通过调用AutoRecorderController类中的接口来读取用户实时数据, 如下:
启动实时数据读取时,需明确表明需要启动哪一种类型的数据实时读取。当前支持实时读取的数据类型仅有:
步数总量:DT_CONTINUOUS_STEPS_TOTAL
华为手机(Android Version >= 10.0)用户申请权限步骤:打开,搜索“HMS Core”,打开“权限”,允许“健身运动”。
非华为手机用户申请权限步骤:。
// 请注意此处的this为Activity对象
final AutoRecorderController controller = HuaweiHiHealth.getAutoRecorderController(this);
// 创建数据回调对象onSamplePointListener打印步数数据
private OnSamplePointListener onSamplePointListener = new OnSamplePointListener() {
@Override
public void onSamplePoint(SamplePoint samplePoint) {
for (Field field : samplePoint.getDataType().getFields()) {
Log.i("AutoRecorderTest", "Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field));
}
}
};
// 以DT_CONTINUOUS_STEPS_TOTAL为例,启动总步数的实时读取,总步数将以0步开始,上报用户此后的总步数
// 总步数将以固定时间间隔实时回调数据给用户,直至用户结束读取,总步数将停止回调
DataType dataType = DataType.DT_CONTINUOUS_STEPS_TOTAL;
controller.startRecord(dataType, onSamplePointListener).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> taskResult) {
// 接口不会一直返回成功,所以需要增加异常回调的处理,目前接口失败的原因包括:应用没有授权,类型不支持
if (taskResult.isSuccessful()) {
Log.i("AutoRecorderTest","onComplete startRecordByType Successful");
} else {
Log.i("AutoRecorderTest","onComplete startRecordByType Failed");
}
}
}).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Log.i("AutoRecorderTest","onSuccess startRecordByType Successful");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i("AutoRecorderTest","onFailure startRecordByType Failed: " + e.getMessage());
}
}); 停止实时数据读取时,需明确表明需要停止哪一种类型的数据实时读取。当前仅支持停止实时读取的数据类型有:
步数总量:DT_CONTINUOUS_STEPS_TOTAL
// 请注意此处的this为Activity对象
final AutoRecorderController controller = HuaweiHiHealth.getAutoRecorderController(this);
// 创建回调对象onSamplePointListener
private OnSamplePointListener onSamplePointListener = new OnSamplePointListener() {
@Override
public void onSamplePoint(SamplePoint samplePoint) {
}
};
// 调用停止实时数据读取接口,传入待停止数据类型DT_CONTINUOUS_STEPS_TOTAL
controller.stopRecord(DataType.DT_CONTINUOUS_STEPS_TOTAL, onSamplePointListener)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> taskResult) {
// 接口不会一直返回成功,所以需要增加异常回调的处理,目前接口失败的原因包括:应用没有授权,类型不支持
if (taskResult.isSuccessful()) {
Log.i("AutoRecorderTest","onComplete stopRecordByType Successful");
} else {
Log.i("AutoRecorderTest","onComplete stopRecordByType Failed");
}
}
})
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Log.i("AutoRecorderTest","onSuccess stopRecordByType Successful");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i("AutoRecorderTest","onFailure stopRecordByType Failed: " + e.getMessage());
}
});