我的应用程序获得了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 running distance
感谢您的帮助。 :)
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;
}
答案 0 :(得分:0)
bucketByActivitySegment
是通过活动来获得结果的关键。您可以通过不同类型的活动来获取存储桶。
前提是存储的值包含活动段。
Here是我发布的类似答案。
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
.bucketByActivitySegment( 1, TimeUnit.MINUTES) // just segement time over 1 minute will be list
.setTimeRange(startTimeLong, endTimeLong, TimeUnit.MILLISECONDS)
.enableServerQueries()
.build();
Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedInAccount(context))
.readData(readRequest)
.addOnSuccessListener()
.addOnFailureListener();