Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 仅限Google Fit步行距离(不包括步行、骑自行车和跑步)_Android_Google Fit_Google Fit Sdk - Fatal编程技术网

Android 仅限Google Fit步行距离(不包括步行、骑自行车和跑步)

Android 仅限Google Fit步行距离(不包括步行、骑自行车和跑步),android,google-fit,google-fit-sdk,Android,Google Fit,Google Fit Sdk,我正在为我的应用程序获取Google Fit距离,但我注意到它包括自行车和跑步。有没有办法只走一段路 这是我的应用程序示例: 及 受保护的Void doInBackground(整数…参数){ daysIndex=params[0]; DataReadRequest DataReadRequest=getDataDistanceReadRequest(daysIndex); //调用History API获取查询数据并等待查询结果 //读取请求。 DataReadResult DataRea

我正在为我的应用程序获取Google Fit距离,但我注意到它包括自行车和跑步。有没有办法只走一段路

这是我的应用程序示例:

受保护的Void doInBackground(整数…参数){
daysIndex=params[0];
DataReadRequest DataReadRequest=getDataDistanceReadRequest(daysIndex);
//调用History API获取查询数据并等待查询结果
//读取请求。
DataReadResult DataReadResult=
Fitness.HistoryApi.readData(client,dataReadRequest).wait(1,TimeUnit.MINUTES);
List bucketList=dataReadResult.getBuckets();
用于(桶:桶列表){
if(bucket.getActivity().equals(“walking”)){
dataSet=(bucket.getDataSet(DataType.TYPE_DISTANCE_DELTA));
}
}
返回null;
}

bucketByActivitySegment
是获取按活动排序结果的关键。您可以按不同类型的活动对桶进行排序

前提是存储的值包含活动段

与我发布的答案类似

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();

哪个对象保存了用户界面中显示的距离值响应?您好@noogui,谢谢您的回复。不确定我是否正确理解了您的问题,但我在UI上显示的距离值来自以下行:
totalSet.getDataPoints().get(0).getValue(Field.Field_distance).asFloat()。出于某种原因,我一直在获取聚合值。嗨@noogui。我创建了一个与我的应用程序类似的示例项目,以防有帮助?再次感谢您的关注。:)您好@ginnyhuang,非常感谢您的回复。我已经按照您的建议尝试使用bucketbysegment,并将我的代码添加到问题的描述中,但我的数据集现在为空?我也试着按时间保持水桶,并获得它的活动,但它一直返回“在车内”,即使我只是步行或骑自行车。你启动了谷歌Fit的运动检测吗?如果你自己开始检测,你可能会丢失一些要记录的东西。我要求的登录和权限与你的大不相同。我不确定这是否会影响到你。我是用这里的指南做的:。在FitnessOptions中,我被要求对DataType.TYPE_ACTIVITY_段、DataType.TYPE_ACTIVITY_示例具有读/写权限。如果您需要距离,您可以添加类型_distance_DELTA、聚合_distance_DELTA。谢谢@ginnyhuang。是的,似乎有很多代码变体,我的代码与官方文档有些不同。不幸的是,我真的不能让这部分工作
DataReadRequest readRequest=new DataReadRequest.Builder()
.aggregate(估计的步长增量,数据类型。聚合距离增量)
.bucketByActivitySegment(7,TimeUnit.DAYS)
.aggregate(活动段,数据类型。聚合活动摘要)
.setTimeRange(startTime,endTime,
.build();
将继续处理该问题。谢谢。嗨,不,我一直不知道怎么做。不,对不起。
 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;
    }
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();