Courtesy: GitHub

Android Fit History Api Sample

A simple example of how to use the History API on the Android Fit platform.


Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

NOTE: You must register an Android client underneath a Google Project in order for the Google Fit API to become available for your app. The process ensures your app has proper consent screen information for users to accept, among other things required to access Google APIs. See the instructions for more details:


The most common problem using these samples is a SIGN_IN_FAILED exception. Users can experience this after selecting a Google Account to connect to the FIT API. If you see the following in logcat output then make sure to register your Android app underneath a Google Project as outlined in the instructions for using this sample at:

10-26 14:40:37.082 1858-2370/? E/MDM: [138] Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}

Use the following channels for support:

If you've found an error in this sample, please file an issue:

Patches are encouraged, and may be submitted according to the instructions in


Copyright 2014 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compileSdkVersion 26

applicationId ""

minSdkVersion 14

targetSdkVersion 26

versionCode 2

versionName "1.1"

compile "${google_play_services_library}"

compile "${google_play_services_library}"

compile "${google_support_library}"

compile "${google_support_library}"

compile fileTree(dir: 'libs', include: ['*.jar'])



This sample demonstrates how to use the History API of the Google Fit platform to insert data, query against existing data, and remove data. It also demonstrates how to authenticate a user with Google Play Services and how to properly represent data in a {@link DataSet}.

Inserts and reads data by chaining {@link Task} from {@link #insertData()} and {@link


Creates a {@link DataSet} and inserts it into user's Google Fit history.

Asynchronous task to read the history data. When the task succeeds, it will print out the data.

Creates and returns a {@link DataSet} of step count data for insertion using the History API.

Returns a {@link DataReadRequest} for all step count changes in the past week.

Logs a record of the query result. It's possible to get more constrained data sets by specifying a data source or data type, but for demonstrative purposes here's how one would dump all the data. In this sample, logging also prints to the device screen, so we can see what the query returns, but your app should not log fitness information as a privacy consideration. A better option would be to dump the data you receive to a local data directory to avoid exposing it to other applications.

Deletes a {@link DataSet} from the History API. In this example, we delete all step count data for the past 24 hours.

Updates and reads data by chaning {@link Task} from {@link #updateData()} and {@link


Creates a {@link DataSet},then makes a {@link DataUpdateRequest} to update step data. Then invokes the History API with the HistoryClient object and update request.

Creates and returns a {@link DataSet} of step count data to update.

Clears all the logging message in the LogView.

Initializes a custom log class that outputs both to in-app targets and logcat.