我正在嘗試將我的應用程序連接到Google Fit。我正在使用需要執行以下操作的IntentService。獲取有關步驟的信息時開始。在這一點上,我試圖通過調用以下代碼來創建GoogleApiClient:在Android應用程序中處理GoogleFit背景
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,並添加步驟的詳細信息爲數據點elemnets後,我同步的信息,谷歌契合,關閉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健身發佈數據。如果在創建GoogleApiClient時使用enableAutoManage,則此行爲會發生變化。但是,爲了啓用客戶端的自動管理,我需要有一個ActivityFragment,由於enableAutoManage所需的參數。我無法訪問IntentyService中的ActivityFragment,並且我希望將客戶端和插入操作的管理保留在可以在後臺運行的單獨服務中。
此外,當我不使用enableAutoManage,即使我已經註冊了GoogleApiClient的連接回調沒有任何反應。
如何確保我的應用程序提示用戶允許應用發佈到Google健身?如果應用無權在用戶打開該應用時在Google健身中發帖,我需要這樣做。有任何想法嗎?謝謝。