Я получаю расстояние Google Fit для своего приложения, но я заметил, что оно включает в себя езду на велосипеде и бег.Есть ли способ получить только пройденное расстояние?
Вот пример для моего заявления: https://github.com/francislainy/fit
// ----------- Google Fit Daily DISTANCE -----------
public static void subscribeDailyDistance() {
Log.d(LOG_TAG, "subscribeDailyDistance was called");
if (client != null) {
// To create a subscription, invoke the Recording API.
// As soon as the subscription is active, fitness data will start recording
Fitness.RecordingApi.subscribe(client, DataType.TYPE_DISTANCE_DELTA)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
if (status.getStatusCode() == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.d(LOG_TAG, "Existing subscription for activity detected.");
} else {
Log.d(LOG_TAG, "Successfully subscribed");
}
// :)
readDistanceToday();
} else {
Log.e(LOG_TAG, "There was a problem subscribing");
}
}
});
}
}
public static class VerifyDataTaskDistance extends AsyncTask<GoogleApiClient, Void, Void> {
float total = 0;
protected Void doInBackground(GoogleApiClient... clients) {
PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(clients[0], DataType.TYPE_DISTANCE_DELTA);
DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
total = totalSet.isEmpty()
? 0
: totalSet.getDataPoints().get(0).getValue(Field.FIELD_DISTANCE).asFloat();
} else {
Log.e(LOG_TAG, "There was a problem getting the distance count");
}
Log.i(LOG_TAG, "Total distance: " + total);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// UI
updateDistanceGoogle(total, MainActivity.mainActivity.getSupportFragmentManager());
}
}
Это похоже на этот другой вопрос SO, на который нет ответа:
Google Fit SDK пробег
Спасибо за вашу помощь.:)
PS: добавление дополнительного кода в соответствии с предложением ginnyhuang использовать сегмент по сегментам
private static DataReadRequest getDataDistanceReadRequest(int numberOfDaysPast) {
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_YEAR, -numberOfDaysPast);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
.setDataType(DataType.TYPE_DISTANCE_DELTA)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("merge_distance_delta")
.setAppPackageName("com.google.android.gms")
.build();
return new DataReadRequest.Builder()
.aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_DISTANCE_DELTA)
//.bucketByTime(1, TimeUnit.DAYS) //todo
.bucketByActivitySegment(7, TimeUnit.DAYS)
.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
}
и
protected Void doInBackground(Integer... params) {
daysIndex = params[0];
DataReadRequest dataReadRequest = getDataDistanceReadRequest(daysIndex);
// Invoke the History API to fetch the data with the query and await the result of
// the read request.
DataReadResult dataReadResult =
Fitness.HistoryApi.readData(client, dataReadRequest).await(1, TimeUnit.MINUTES);
List<Bucket> bucketList = dataReadResult.getBuckets();
for (Bucket bucket : bucketList) {
if (bucket.getActivity().equals("walking")) {
dataSet = (bucket.getDataSet(DataType.TYPE_DISTANCE_DELTA));
}
}
return null;
}