Google Fit Android API Acitivity Time?

南笙酒味 提交于 2019-12-23 04:43:56

问题


I am currently using the Google Fit Android API to access step count data within my application. On the Google fit android app it has a value for active time Attached image of the value I am trying to access

I have tried numerous ways to access this data within my app but I can't figure out which of the instantaneous data types I should be using ? or if I'm completely wrong and need to try something different?

Any help would be greatly appreciated. Thanks


回答1:


I had the same doubt and I used this reference https://developers.google.com/fit/android/history I think I have the possible solution, in this case I set the time interval from midnight to now. You need to create a DataReadRequest using 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]
}

The activeTime variable shows the Active Time of every Fitness activity, you should take a look this reference https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessActivities because one of the listed activities is STILL: The user is still (not moving).

I hope this has helped you.




回答2:


Each Data Set in google fit have start time and end time . You can just get the time spend by only Substracting them . Below is an example.

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


来源:https://stackoverflow.com/questions/34678417/google-fit-android-api-acitivity-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!