Google Fit Android API活动时间?
我目前正在使用Google Fit Android API访问我的应用程序中的步骤计数数据。在Google fit android应用程序上,它有一个活动时间值 我已经尝试了很多方法在我的应用程序中访问这些数据,但我不知道应该使用哪种即时数据类型?或者如果我完全错了,需要尝试不同的东西Google Fit Android API活动时间?,android,google-fit,Android,Google Fit,我目前正在使用Google Fit Android API访问我的应用程序中的步骤计数数据。在Google fit android应用程序上,它有一个活动时间值 我已经尝试了很多方法在我的应用程序中访问这些数据,但我不知道应该使用哪种即时数据类型?或者如果我完全错了,需要尝试不同的东西 任何帮助都将不胜感激。谢谢我也有同样的疑问,我使用了这个参考,我想我有可能的解决办法,在这种情况下,我设置了从午夜到现在的时间间隔。您需要使用bucketByActivitySegment private cla
任何帮助都将不胜感激。谢谢我也有同样的疑问,我使用了这个参考,我想我有可能的解决办法,在这种情况下,我设置了从午夜到现在的时间间隔。您需要使用
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]
}
私有类VerifyDataTask扩展了AsyncTask{
@凌驾
受保护的空位背景(空位…空位){
//首先创建查询。
DataReadRequest readRequest=queryFitnessData();
//[开始读取数据集]
//调用History API获取查询数据并等待查询结果
//读取请求。
DataReadResult DataReadResult=
Fitness.HistoryApi.readData(mClient,readRequest).wait(1,TimeUnit.MINUTES);
//[结束读取数据集]
//为了便于示例,我们将打印数据,以便查看刚才添加的内容。
//一般来说,出于隐私原因,应避免记录健身信息。
打印数据(dataReadResult);
返回null;
}
}
公共静态DataReadRequestQueryFitnessData(){
//[开始生成读取数据请求]
//在此时刻之前使用1周的范围设置开始和结束日期。
Calendar cal=Calendar.getInstance();
现在日期=新日期();
校准设定时间(现在);
long-endTime=cal.getTimeInMillis();
校准设置(日历小时/天,0);
校准设置(日历分钟,0);
校准设置(日历秒,0);
long startTime=cal.getTimeInMillis();
DataSource ACTIVITY_SEGMENT=新建DataSource.Builder()
.setDataType(数据类型.TYPE\U活动\U段)
.setType(数据源.TYPE_派生)
.setStreamName(“估计的活动部分”)
.setAppPackageName(“com.google.android.gms”)
.build();
DataReadRequest=newDataReadRequest.Builder()
.aggregate(活动\段,数据类型。聚合\活动\摘要)
.bucketByActivitySegment(1,时间单位:秒)
.setTimeRange(开始时间、结束时间、时间单位.毫秒)
.build();
//[结束构建读取数据请求]
返回读取请求;
}
公共静态void打印数据(DataReadResult DataReadResult){
//[开始解析\读取\数据\结果]
//如果DataReadRequest对象指定了聚合数据,则将返回dataReadResult
//作为包含数据集的存储桶,而不仅仅是数据集。
如果(dataReadResult.GetBucket().size()>0){
Log.i(标记,“返回的数据集存储桶数为:”
+dataReadResult.getbucket().size());
for(Bucket:dataReadResult.getbucket()){
Log.i(标记,bucket.getActivity());
long-activeTime=bucket.getEndTime(TimeUnit.MINUTES)-bucket.getStartTime(TimeUnit.MINUTES);
Log.i(标记“活动时间”+活动时间);
}
}
//[结束解析\读取\数据\结果]
}
activeTime变量显示每个健身活动的活动时间,您应该查看此参考,因为列出的活动之一是静止
:用户静止(不移动)
我希望这对您有所帮助。google fit中的每个数据集都有开始时间和结束时间。你只需减去它们就可以得到所花的时间。下面是一个例子
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);
}
}