Google适合AndroidAPI活动时间?
Google Fit Android API Acitivity Time?
我目前正在使用 Google Fit Android API 访问我的应用程序中的步数数据。在 Google fit android 应用程序上,它有一个活动时间值 Attached image of the value I am trying to access
我已经尝试了多种方法在我的应用程序中访问这些数据,但我无法弄清楚我应该使用哪种即时数据类型?或者如果我完全错了需要尝试不同的东西?
如有任何帮助,我们将不胜感激。谢谢
我有同样的疑问,我使用了这个参考https://developers.google.com/fit/android/history我想我有可能的解决方案,在这种情况下,我将时间间隔设置为从午夜到现在。您需要使用 bucketByActivitySegment
创建 DataReadRequest
private class VerifyDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
// Begin by creating the query.
DataReadRequest readRequest = queryFitnessData();
// [START read_dataset]
// Invoke the History API to fetch the data with the query and await the result of
// the read request.
DataReadResult dataReadResult =
Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);
// [END read_dataset]
// For the sake of the sample, we'll print the data so we can see what we just added.
// In general, logging fitness information should be avoided for privacy reasons.
printData(dataReadResult);
return null;
}
}
public static DataReadRequest queryFitnessData() {
// [START build_read_data_request]
// Setting a start and end date using a range of 1 week before this moment.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long startTime = cal.getTimeInMillis();
DataSource ACTIVITY_SEGMENT = new DataSource.Builder()
.setDataType(DataType.TYPE_ACTIVITY_SEGMENT)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_activity_segment")
.setAppPackageName("com.google.android.gms")
.build();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.bucketByActivitySegment(1, TimeUnit.SECONDS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
// [END build_read_data_request]
return readRequest;
}
public static void printData(DataReadResult dataReadResult) {
// [START parse_read_data_result]
// If the DataReadRequest object specified aggregated data, dataReadResult will be returned
// as buckets containing DataSets, instead of just DataSets.
if (dataReadResult.getBuckets().size() > 0) {
Log.i(TAG, "Number of returned buckets of DataSets is: "
+ dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
Log.i(TAG, bucket.getActivity());
long activeTime = bucket.getEndTime(TimeUnit.MINUTES) - bucket.getStartTime(TimeUnit.MINUTES);
Log.i(TAG, "Active time " + activeTime);
}
}
// [END parse_read_data_result]
}
activeTime 变量显示每个健身的活动时间 activity,你应该看看这个参考 https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessActivities 因为列出的活动之一是 STILL
:用户仍然(不动)。
希望对您有所帮助。
google适合的每个数据集都有开始时间和结束时间。您只需减去它们即可获得花费的时间。下面是一个例子。
if (dataReadResult.getBuckets().size() > 0) {
for (Bucket bucket : dataReadResult.getBuckets()) {
long activeTime = bucket.getEndTime(TimeUnit.MINUTES) - bucket.getStartTime(TimeUnit.MINUTES);
Log.i(TAG, "Active time " + activeTime);
}
}
我目前正在使用 Google Fit Android API 访问我的应用程序中的步数数据。在 Google fit android 应用程序上,它有一个活动时间值 Attached image of the value I am trying to access
我已经尝试了多种方法在我的应用程序中访问这些数据,但我无法弄清楚我应该使用哪种即时数据类型?或者如果我完全错了需要尝试不同的东西?
如有任何帮助,我们将不胜感激。谢谢
我有同样的疑问,我使用了这个参考https://developers.google.com/fit/android/history我想我有可能的解决方案,在这种情况下,我将时间间隔设置为从午夜到现在。您需要使用 bucketByActivitySegment
private class VerifyDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
// Begin by creating the query.
DataReadRequest readRequest = queryFitnessData();
// [START read_dataset]
// Invoke the History API to fetch the data with the query and await the result of
// the read request.
DataReadResult dataReadResult =
Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);
// [END read_dataset]
// For the sake of the sample, we'll print the data so we can see what we just added.
// In general, logging fitness information should be avoided for privacy reasons.
printData(dataReadResult);
return null;
}
}
public static DataReadRequest queryFitnessData() {
// [START build_read_data_request]
// Setting a start and end date using a range of 1 week before this moment.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long startTime = cal.getTimeInMillis();
DataSource ACTIVITY_SEGMENT = new DataSource.Builder()
.setDataType(DataType.TYPE_ACTIVITY_SEGMENT)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_activity_segment")
.setAppPackageName("com.google.android.gms")
.build();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.bucketByActivitySegment(1, TimeUnit.SECONDS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
// [END build_read_data_request]
return readRequest;
}
public static void printData(DataReadResult dataReadResult) {
// [START parse_read_data_result]
// If the DataReadRequest object specified aggregated data, dataReadResult will be returned
// as buckets containing DataSets, instead of just DataSets.
if (dataReadResult.getBuckets().size() > 0) {
Log.i(TAG, "Number of returned buckets of DataSets is: "
+ dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
Log.i(TAG, bucket.getActivity());
long activeTime = bucket.getEndTime(TimeUnit.MINUTES) - bucket.getStartTime(TimeUnit.MINUTES);
Log.i(TAG, "Active time " + activeTime);
}
}
// [END parse_read_data_result]
}
activeTime 变量显示每个健身的活动时间 activity,你应该看看这个参考 https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessActivities 因为列出的活动之一是 STILL
:用户仍然(不动)。
希望对您有所帮助。
google适合的每个数据集都有开始时间和结束时间。您只需减去它们即可获得花费的时间。下面是一个例子。
if (dataReadResult.getBuckets().size() > 0) {
for (Bucket bucket : dataReadResult.getBuckets()) {
long activeTime = bucket.getEndTime(TimeUnit.MINUTES) - bucket.getStartTime(TimeUnit.MINUTES);
Log.i(TAG, "Active time " + activeTime);
}
}