Courtesy: GitHub

Android Fit History Api Sessions Sample

A simple example of how to use sessions with 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'])



  • android.permission.ACCESS_FINE_LOCATION


This sample demonstrates how to use the Sessions API of the Google Fit platform to insert sessions into the History API, query against existing data, and remove sessions. It also demonstrates how to authenticate a user with Google Play Services and how to properly represent data in a Session, as well as how to use ActivitySegments.

A wrapper for {@link #insertAndVerifySession}. If the user account has OAuth permission, continue to {@link #insertAndVerifySession}, else request OAuth permission for the account.

Checks if user's account has OAuth permission to Fitness API.

Launches the Google SignIn activity to request OAuth permission for the user.

Gets {@link FitnessOptions} in order to check or request OAuth permission for the user.

Creates and executes a {@link SessionInsertRequest} using {@link} to insert a session.

Creates and executes a {@link SessionReadRequest} using {@link} to verify the insertion succeeded .

Inserts and verifies a session by chaining {@link Task} form {@link #insertSession} and {@link #verifySession}.

Creates a {@link SessionInsertRequest} for a run that consists of 10 minutes running, 10 minutes walking, and 10 minutes of running. The request contains two {@link DataSet}s: speed data and activity segments data. {@link Session}s are time intervals that are associated with all Fit data that falls into that time interval. This data can be inserted when inserting a session or independently, without affecting the association between that data and the session. Future queries for that session will return all data relevant to the time interval created by the session. Sessions may contain {@link DataSet}s, which are comprised of {@link DataPoint}s and a {@link DataSource}. A {@link DataPoint} is associated with a Fit {@link DataType}, which may be derived from the {@link DataSource}, as well as a time interval, and a value. A given {@link DataSet} may only contain data for a single data type, but a {@link Session} can contain multiple {@link DataSet}s.

Returns a {@link SessionReadRequest} for all speed data in the past week.

Deletes the {@link DataSet} we inserted with our {@link Session} from the History API. In this example, we delete all step count data for the past 24 hours. Note that this deletion uses the History API, and not the Sessions API, since sessions are truly just time intervals over a set of data, and the data is what we are interested in removing.

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

Returns the current state of the permissions needed.

Callback received when a permissions request has been completed.