Google fit API也会从历史记录中提供步骤计数。如何从API获取距离历史记录?
更多信息:https://developers.google.com/fit/faq
FitnessOptions fitnessOptions =
FitnessOptions.builder()
.addDataType(DataType.TYPE_DISTANCE_DELTA)
.addDataType(DataType.TYPE_STEP_COUNT_DELTA)
.build();
if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
GoogleSignIn.requestPermissions(
this,
REQUEST_OAUTH_REQUEST_CODE,
GoogleSignIn.getLastSignedInAccount(this),
fitnessOptions);
} else {
subscribe();
}
public void subscribe() {
// To create a subscription, invoke the Recording API. As soon as the subscription is
// active, fitness data will start recording.
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.subscribe(DataType.TYPE_STEP_COUNT_CUMULATIVE)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.i(TAG, "Successfully subscribed!");
} else {
Log.w(TAG, "There was a problem subscribing.", task.getException());
}
}
});
}
以下代码读取当前每日步骤总数,从当前设备的午夜计算得出 当前时区。
private void readData() {
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener(
new OnSuccessListener<DataSet>() {
@Override
public void onSuccess(DataSet dataSet) {
long total =
dataSet.isEmpty()
? 0
: dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
Log.i(TAG, "Total steps: " + total);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "There was a problem getting the step count.", e);
}
});
}
因此,尽管有足够的步骤,但我需要以公里为单位来获得距离。
你的帮助可以证明对我们有利。
由于
答案 0 :(得分:0)
如果你想每天获得总距离:
private void readHistoryData() {
// Invoke the History API to fetch the data with the query
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readDailyTotal(DataType.TYPE_DISTANCE_DELTA)
.addOnSuccessListener(new OnSuccessListener<DataSet>() {
@Override
public void onSuccess(DataSet dataSet) {
dumpDataSet(dataSet);
}
});
}
确保当天有健身数据与距离