java.lang.Object | |
↳ | com.google.android.gms.fitness.Fitness |
The main entry point to Google Fit APIs.
The Google Fit APIs help app developers collect and use fitness-related sensor data in their applications. There are several different APIs, each solving a different problem:
SensorsApi
exposes a unified view of sensor streams on the local
device and connected devices, and delivers live events to listeners.
RecordingApi
enables low-battery, always-on background collection
of sensor data into the Google Fit store.
SessionsApi
lets apps create and manage sessions of user activity.
HistoryApi
allows querying and insertion of data in Google Fit.
BleApi
can be used to work with Bluetooth Low Energy devices.
ConfigApi
can be used to access custom data types and settings.
GoalsApi
can be used to read fitness goals created by users in Google Fit.
data type
. Each data type operation requires the
user to have granted the app permission to access and store fitness data for the given data type.
addScope(Scope)
to add the
necessary scopes, which should be selected from the SCOPE_XXX
constants in this class.
To use a specific user account, apps can use setAccountName(String)
,
or use useDefaultAccount()
to use the default account. The specified account
and scopes will be used to acquire the necessary OAuth tokens on behalf of the app.
In case the app does not have the needed OAuth permissions for the requested scopes,
Google Fit will send back a result with status code set
to NEEDS_OAUTH_PERMISSIONS
. In this case, the app should use
startResolutionForResult(Activity, int)
to get the necessary OAuth
permissions.
The first connection to Fit API may require a network connection to verify the account and scopes
associated with it. In case no network connection is available, Google Fit will send back a
result with status code set to NETWORK_ERROR
.
Sample usage of Google Fit Client:
public class MyActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, OnDataPointListener { private static final int REQUEST_OAUTH = 1001; private GoogleApiClient mGoogleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create a Google Fit Client instance with default user account. mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Fitness.SENSORS_API) // Required for SensorsApi calls // Optional: specify more APIs used with additional calls to addApi .useDefaultAccount() .addScope(new Scope(Scopes.FITNESS)) .addOnConnectionsCallbacks(this) .addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect(); } @Override public void onConnected(Bundle connectionHint) { // Connected to Google Fit Client. Fitness.SensorsApi.add( mGoogleApiClient, new SensorRequest.Builder() .setDataType(DataType.STEP_COUNT_DELTA) .build(), this); } @Override public void onDataPoint(DataPoint dataPoint) { // Do cool stuff that matters. } @Override public void onConnectionSuspended(int cause) { // The connection has been interrupted. Wait until onConnected() is called. } @Override public void onConnectionFailed(ConnectionResult result) { // Error while connecting. Try to resolve using the pending intent returned. if (result.getErrorCode() == FitnessStatusCodes.NEEDS_OAUTH_PERMISSIONS) { try { result.startResolutionForResult(this, REQUEST_OAUTH); } catch (SendIntentException e) { } } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_OAUTH && resultCode == RESULT_OK) { mGoogleApiClient.connect(); } }
ACTION_VIEW
defines an intent to view fitness data or a session
ACTION_TRACK
defines an intent to track an activity
ACTION_VIEW_GOAL
defines an intent to view a fitness goal
sessions
, data sources
, data types
, and
activities
. The documentation for each intent action specifies the
different attributes of the intent, including actions, MIME types, and extras. It also
specifies how the intents can be built and parsed using methods in the API.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | ACTION_TRACK | Broadcast action: the user has requested that an application start or stop tracking their activity. | |||||||||
String | ACTION_VIEW | Broadcast action: the user has requested that an application show the value of a particular fitness data type. | |||||||||
String | ACTION_VIEW_GOAL | Broadcast action: the user has requested to view their current fitness goal. | |||||||||
String | EXTRA_END_TIME | Name for the long intent extra containing the end time. | |||||||||
String | EXTRA_START_TIME | Name for the long intent extra containing the start time. |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
API | This field is deprecated. in favor of granular API tokens. | ||||||||||
BLE_API | Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>) to enable BleApi . |
||||||||||
BleApi | Entry point to the Google Fit BLE API . |
||||||||||
CONFIG_API | Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>) to enable ConfigApi . |
||||||||||
ConfigApi | Entry point to the Google Fit Config API . |
||||||||||
GOALS_API | Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>) to enable GoalsApi . |
||||||||||
GoalsApi | Entry point to the Google Fit Goals API . |
||||||||||
HISTORY_API | Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>) to enable HistoryApi . |
||||||||||
HistoryApi | Entry point to the Google Fit History API . |
||||||||||
RECORDING_API | Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>) to enable RecordingApi . |
||||||||||
RecordingApi | Entry point to the Google Fit Recording API . |
||||||||||
SCOPE_ACTIVITY_READ | Scope for read access to activity-related data types in Google Fit, which include: | ||||||||||
SCOPE_ACTIVITY_READ_WRITE | Scope for read/write access to activity-related data types in Google Fit, which include: | ||||||||||
SCOPE_BODY_READ | Scope for read access to the biometric data types in Google Fit, which include: | ||||||||||
SCOPE_BODY_READ_WRITE | Scope for read/write access to biometric data types in Google Fit, which include: | ||||||||||
SCOPE_LOCATION_READ | Scope for read access to location-related data types in Google Fit, which include: | ||||||||||
SCOPE_LOCATION_READ_WRITE | Scope for read/write access to location-related data types in Google Fit, which include: | ||||||||||
SCOPE_NUTRITION_READ | Scope for read access to the nutrition and hydration data types in Google Fit, which include: | ||||||||||
SCOPE_NUTRITION_READ_WRITE | Scope for read/write access to nutrition and hydration data types in Google Fit, which include: | ||||||||||
SENSORS_API | Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>) to enable SensorsApi . |
||||||||||
SESSIONS_API | Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>) to enable SessionsApi . |
||||||||||
SensorsApi | Entry point to the Google Fit Sensors API . |
||||||||||
SessionsApi | Entry point to the Google Fit Sessions API . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Retrieves the end time extra from the given intent.
| |||||||||||
Retrieves the start time extra from the given intent.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Broadcast action: the user has requested that an application start or stop tracking their activity. The intent will include the following attributes:
MIME_TYPE_PREFIX
followed by the name of the activity. Apps can use a MIME type filter to listen
only on activities they can track.
getMimeType(String)
can be used to generate a MIME
type from an activity.
Extra EXTRA_STATUS
: an extra indicating the current status
of the activity (active or completed).
Broadcast action: the user has requested that an application show the value of a particular fitness data type. This could be an intent to visualize the current value of a data type (such as the current heart rate), or the value of a data type over a period of time. The extras will determine what the particular intent is.
The intent will include the following attributes:MIME_TYPE_PREFIX
followed by
the name of the data type the user would like to visualize. Apps can use a MIME type
filter to listen only on data types they can visualize. The MIME type can be generated
by getMimeType(String)
.
Extra EXTRA_START_TIME
: an extra indicating the start time in
milliseconds since epoch, present if the user desires to visualize data over a period
of time. The start time can be extracted by getStartTime(Intent, TimeUnit)
.
Extra EXTRA_END_TIME
: an extra indicating the end time in
milliseconds since epoch, present if the user desires to visualize data over a period
of time. If end time isn't specified, but start time is, then the end time used
should be "now". The end time can be extracted by getEndTime(Intent, TimeUnit)
.
Extra EXTRA_DATA_SOURCE
: an optional extra indicating the
specific data source the user would like to visualize, if any. It can be extracted
using extract(Intent)
.
Broadcast action: the user has requested to view their current fitness goal.
Name for the long intent extra containing the end time. It can be extracted using
getEndTime(Intent, TimeUnit)
Name for the long intent extra containing the start time. It can be extracted using
getStartTime(Intent, TimeUnit)
.
This field is deprecated.
in favor of granular API tokens.
Instead of API, you now need to use the specific API for the calls you're making, .e.g.
// Create a Google Fit Client instance with default user account. mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Fitness.SENSORS_API) // Required only if you're making SensorsApi calls .addApi(Fitness.RECORDING_API) // Required only if you're making RecordingApi calls .addApi(Fitness.API) // Required only if you're making HistoryApi calls .addApi(Fitness.SESSIONS_API) // Required only if you're making SessionsApi calls // Optional: request more APIs with additional calls to addApi .useDefaultAccount() .addScope(new Scope(Scopes.FITNESS)) .addOnConnectionsCallbacks(this) .addOnConnectionFailedListener(this) .build();
Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>)
to enable BleApi
.
Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>)
to enable ConfigApi
.
Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>)
to enable GoalsApi
.
Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>)
to enable HistoryApi
.
Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>)
to enable RecordingApi
.
Scope for read access to activity-related data types in Google Fit, which include:
Scope for read/write access to activity-related data types in Google Fit, which include:
Scope for read access to the biometric data types in Google Fit, which include:
Scope for read/write access to biometric data types in Google Fit, which include:
Scope for read access to location-related data types in Google Fit, which include:
Scope for read/write access to location-related data types in Google Fit, which include:
Scope for read access to the nutrition and hydration data types in Google Fit, which include:
Scope for read/write access to nutrition and hydration data types in Google Fit, which include:
Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>)
to enable SensorsApi
.
Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>)
to enable SessionsApi
.
Retrieves the end time extra from the given intent.
Parameters | |
---|---|
intent |
Intent :
the intent to extract the end time from |
timeUnit |
TimeUnit :
the desired time unit for the returned end time |
Returns | |
---|---|
long |
the end time, in time unit since epoch, or -1 if not found
|
Retrieves the start time extra from the given intent.
Parameters | |
---|---|
intent |
Intent :
the intent to extract the start time from |
timeUnit |
TimeUnit :
the desired time unit for the returned start time |
Returns | |
---|---|
long |
the start time, in time unit since epoch, or -1 if not found
|