【发布时间】:2016-04-12 11:47:12
【问题描述】:
我正在尝试将我的应用连接到 Google 健身。我正在使用需要执行以下操作的 IntentService。当我有关于步骤的信息时开始。此时我正在尝试通过调用以下代码来创建 GoogleApiClient:
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
log.info("FITNESS_API: Connected!!!");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
insertOrUpdateDataPoints();
}
});
thread.start();
}
@Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
log.info("FITNESS_API: Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
log.info("FITNESS_API: Connection lost. Reason: Service Disconnected");
}
}
}
).build();
mClient.connect();
在创建 DataSet 并将步骤详细信息添加为 DataPoint elemnets 后,我将信息同步到 Google Fit 并关闭 GoogleApiClient:
com.google.android.gms.common.api.Status insertStatus =
Fitness.HistoryApi.insertData(mClient, dataSet).await(1, TimeUnit.MINUTES);
// Before querying the data, check to see if the insertion succeeded.
if (!insertStatus.isSuccess()) {
log.info("FITNESS_API: There was a problem inserting the dataset. Status = " + insertStatus.getStatusCode());
}
mClient.disconnect();
mClient = null;
问题在于,通过尝试自行管理 GoogleApiClient(没有 enableAutoManage),我没有收到允许应用将数据发布到 Google Fit 的提示。如果我在创建 GoogleApiClient 时使用 enableAutoManage,此行为会发生变化。但是,为了为客户端启用 AutoManage,由于 enableAutoManage 所需的参数,我需要一个 ActivityFragment。我无权访问 IntentyService 中的 ActivityFragment,我确实希望将客户端的管理和插入操作保留在可以在后台运行的单独服务中。
此外,即使我已经为 GoogleApiClient 注册了连接回调,但我不使用 enableAutoManage 时也没有任何反应。
如何确保我的应用程序提示用户允许该应用程序发布到 Google Fit?如果用户打开应用时应用无权在 Google Fit 中发帖,我需要这样做。有任何想法吗?谢谢。
【问题讨论】:
标签: android google-fit