我正在制作一个健身应用,我需要在其中获取用户移动的步数和距离。为此我通过订阅步数和距离来使用Google fit RECORDING_API,我成功地获得了步数,但无法获得距离。这是我的代码
Fitness.RecordingApi.subscribe(googleApiClient, DataType.TYPE_DISTANCE_DELTA)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
if (status.getStatusCode() == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.e( "RecordingAPI", "Already subscribed to get Distance Recording API");
} else {
Log.e("RecordingAPI", "Subscribed to get distance using Recording API");
}
}
new FetchDistanceAsync().execute();
}
});
private class FetchDistanceAsync extends AsyncTask<Object, Object, Float>{
@Override
protected Float doInBackground(Object... objects) {
float distance = 0;
PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(googleApiClient, DataType.TYPE_DISTANCE_DELTA);
DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
if (totalSet != null) {
distance = totalSet.isEmpty() ? 0 : totalSet.getDataPoints().get(0).getValue(Field.FIELD_DISTANCE).asFloat();
}
} else {
Log.w(TAG, "There was a problem getting the calories.");
}
return distance;
}
@Override
protected void onPostExecute(Float aLong) {
super.onPostExecute(aLong);
float distance = aLong/1000;
double dis = Math.round(distance * 100.0) / 100.0;
tvKilometer.setText(String.valueOf(dis));
}
}
距离回到了我0.实际上我移动了184步但仍然距离返回值0.你能帮我解决这个问题。