繁体   English   中英

Google Fit Android API激活时间?

[英]Google Fit Android API Acitivity Time?

我目前正在使用Google Fit Android API访问应用程序中的步数数据。 在Google Fit android应用上,它具有有效时间值我试图访问的值的附加图像

我尝试了多种方法来访问我的应用程序中的这些数据,但是我不知道应该使用哪种瞬时数据类型? 还是我完全错了,需要尝试一些不同的方法?

任何帮助将不胜感激。 谢谢

我也有同样的疑问,我使用了此参考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变量显示每个健身活动的活动时间,您应该查看此参考https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessActivities,因为其中列出了一项活动是STILL :用户仍在(不动)。

希望对您有所帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM