Fitness.API 已在 Google Play 服务 7.0 中删除?

Fitness.API removed in Google Play Services 7.0?

升级到 Google Play Services 7.0 后,我的 GoogleApiClient 连接到 Google Fit 的代码不再有效:它说:

Error:(87, 21) error: no suitable method found for addApi(Void) method Builder.addApi(Api,O) is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length) method Builder.addApi(Api) is not applicable (actual argument Void cannot be converted to Api by method invocation conversion) where O is a type-variable: O extends HasOptions declared in method addApi(Api,O)

我构建 GoogleApiClient 的代码是:

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Fitness.API)
    .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

根据 Google Play services 7.0 blog post:

The previous Fitness.API that you passed into your GoogleApiClient has now been replaced with a number of APIs, matching the high level set of Google Fit Android APIs:

  • SENSORS_API to access raw sensor data via SensorsApi
  • RECORDING_API to record data via RecordingApi
  • HISTORY_API for inserting, deleting, or reading data via HistoryApi
  • SESSIONS_API for managing sessions via SessionsApi
  • BLE_API to interact with Bluetooth Low Energy devices via BleApi
  • CONFIG_API to access custom data types and settings for Google Fit via ConfigApi

因此,您应该更新 GoogleApiClient 以添加您使用的所有合适的 API。例如,如果您同时使用 SensorsApiRecordingApi,您的代码应如下所示:

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Fitness.SENSORS_API)
    .addApi(Fitness.REPORTING_API)
    .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

注意:针对较早版本的 Google Play 服务编译的应用程序将继续工作,但不会获得上述 Google Play 服务 7.0 中拆分 API 带来的内存优势在同一个博客中 post:

This change significantly reduces the memory requirement for Google Fit enabled apps running in the background. Like always, apps built on previous versions of Google Play services will continue to work, but we strongly suggest you rebuild your Google Fit enabled apps to take advantage of this change.