Getting list of activities(Movements) from google fit api

筅森魡賤 提交于 2019-11-30 21:54:09

The Google Fit activities are listed in the FitnessActivities class.

You can programmatically get a list of all these fields using:

FitnessActivities.class.getFields()

Had similar problem when started playing with Google Fit API on Android.

There are videos with code samples as well as more detailed API documentation on Google Fit website.

It helped me a lot -- https://developers.google.com/fit/android/get-started

Check both videos and later how to save and get data types:

https://developers.google.com/fit/android/data-types

To have some data available install Google Fit app on your android phone. Use it for a while and then you will have some real data in Google Fit database available.

EDIT:

If I get your edited question correctly, then you need something like the following code. Please note that I use this in my own app that lists activities recorded by Google Fit Andorid app. I'm not sure if it will list other activities, for example custom data types recorded by other apps.

Request "activites" (like STILL, RUNNING, WALKING) from Google Fit:

        DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_ACTIVITY_SEGMENT)
            // maybe you want to limit data to specific time range?
            //.setTimeRange(today.startTime, today.endTime, TimeUnit.MILLISECONDS)
            .build();

Then parse the response. While parsing there will be activity time available:

        Fitness.HistoryApi.readData(mClient, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
        @Override
        public void onResult(DataReadResult dataReadResult) {
            for (DataSet dataSet : dataReadResult.getDataSets()) {
                for (DataPoint dataPoint : dataSet.getDataPoints()) {
                    DataType dataType = dataPoint.getDataType();
                    if (dataType.equals(DataType.TYPE_ACTIVITY_SEGMENT)) {
                        String activity = FitnessActivities.getValue(dataPoint);

                        /* process as needed */
                        /* the `activitity' string contains values as described here:
                         * https://developer.android.com/reference/com/google/android/gms/fitness/FitnessActivities.html
                         */

                    }
                }
            }
        }
    });

Like I said it works for me -- in my own app I list activities (and their type, ie. walking, running, etc) recorded by Google Fit app for Android.

parth Patel

Hope this can help others...

List<Session> sessions = sessionReadResponse.getSessions();

for (Session session : sessions) {
    dumpSession(session);
    Log.i(TAG, "Activity Name: "+sessions.get(position).getActivity());
    position++;
    List<DataSet> dataSets = sessionReadResponse.getDataSet(session);
    for (DataSet dataSet : dataSets) {
        dumpDataSet(dataSet);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!