Google Fit Android API活动时间?

时间:2016-01-08 13:37:45

标签: android google-fit

我目前正在使用Google Fit Android API访问我的应用程序中的步数数据。在Google fit Android应用中,它具有活动时间Attached image of the value I am trying to access

的值

我已尝试过多种方法在我的应用程序中访问此数据,但我无法弄清楚我应该使用哪种即时数据类型?或者如果我完全错了,需要尝试不同的东西?

非常感谢任何帮助。感谢

2 个答案:

答案 0 :(得分:1)

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

我希望这对你有帮助。

答案 1 :(得分:0)

google fit中的每个数据集都有开始时间和结束时间。你只需通过Substract它就可以获得时间。以下是一个例子。

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