Getting Started on Android

This guide explains how to start developing with Google Fit on Android. Google Fit is part of Google Play services.

Step 1: Get a Google Account

To use the Google Fit APIs, you need a Google Account. If you already have an account, then you're all set. You may also want to create a separate Google Account for testing purposes.

Step 2: Get Google Play services

Google Fit is available on Android devices with Google Play services 7.0 or higher. Devices running Android 2.3 or higher that have the Google Play Store app automatically receive updates to Google Play services.

To check which version of Google Play services is installed on your device, go to Settings > Apps > Google Play services.

Ensure that you have the latest client library for Google Play services on your development host:

  1. Open the Android SDK Manager.
  2. Under SDK Tools, find Google Play services and Google Repository.
  3. If the status for these packages is different than Installed, select them both and click Install Packages.

Step 3: Get an OAuth 2.0 client ID

If you haven't already enabled the Fitness API and obtained an OAuth 2.0 client ID, follow the instructions in Get an OAuth 2.0 Client ID to do so now.

Step 4: Configure your project

Android Studio is the recommended development environment for building an app with the Fitness API. To create a new project, follow the steps described in Creating a Project.

In the IDE, open the build.gradle file for your module and add the Google Play services client library as a dependency:

apply plugin: 'com.android.application'
...

dependencies {
    compile 'com.google.android.gms:play-services-fitness:11.6.0'
    compile 'com.google.android.gms:play-services-auth:11.6.0'
}

Step 5: Connect to the fitness service

Before you can invoke methods from the Google Fit APIs, you must connect to one or more of the following API clients, which are part of Google Play services:

For more information on these APIs, see the Android APIs Overview.

When your app requests a connection to the service, it specifies the data type(s) and access that it needs. Google Fit asks the user to grant permission to your app to access their fitness data. For more information about scopes and authorization, see Authorization.

Create the API client as follows:

  1. Create a FitnessOptions instance, declaring the Fit API data types and access required by your app:
    FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .build();
  2. Check if the user has previously granted the necessary data access, and if not, initiate the authorization flow:
    if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
        GoogleSignIn.requestPermissions(
                this, // your activity
                GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
                GoogleSignIn.getLastSignedInAccount(this),
                fitnessOptions);
    } else {
        accessGoogleFit();
    }
  3. If the authorization flow is required, handle the user's response:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == GOOGLE_FIT_PERMISSIONS_REQUEST_CODE) {
                accessGoogleFit();
            }
        }
    }
  4. After the user has authorized access to the data requested, create the desired GoogleApi client (e.g. a HistoryClient to read and/or write historic fitness data) for the purposes of your app:

    private void accessGoogleFit() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.YEAR, -1);
        long startTime = cal.getTimeInMillis();

    DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA) .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS) .build();

    Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this)) .readData(readRequest) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(DataReadResponse dataReadResponse) { Log.d(LOG_TAG, "onSuccess()"); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.e(LOG_TAG, "onFailure()", e); } }) .addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { Log.d(LOG_TAG, "onComplete()"); } }); }

发送以下问题的反馈:

此网页
Google Fit
Google Fit