Google Play Services 7.0中删除了Fitness.API?

4
升级到Google Play Services 7.0 后,我的连接 Google Fit 的 GoogleApiClient 代码不再起作用,出现以下错误:

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();
1个回答

12
根据Google Play services 7.0博客文章,您之前传递到 GoogleApiClient 中的Fitness.API现已被一些API替换,这些API与Google Fit Android API的高级集合相匹配:
- SENSORS_API可通过SensorsApi访问原始传感器数据。 - RECORDING_API可通过RecordingApi记录数据。 - HISTORY_API用于插入、删除或读取数据,可通过HistoryApi访问。 - SESSIONS_API可管理会话,可通过SessionsApi实现。 - BLE_API可通过BleApi与蓝牙低功耗设备进行交互。 - CONFIG_API可通过ConfigApi访问Google Fit的自定义数据类型和设置。
因此,您应该更新您的 GoogleApiClient 以添加您使用的所有适当的API。例如,如果您同时使用 SensorsApi RecordingApi ,则您的代码应如下所示:
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相关的内存优势,这也在同一篇博客文章中提到:

此更改显着减少了后台运行的启用Google Fit的应用程序所需的内存。像往常一样,基于先前版本的Google Play服务构建的应用程序将继续工作,但我们强烈建议您重新构建您的启用Google Fit的应用程序以利用此更改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接