我正在开发一款使用google fit来收集燃烧卡路里的应用。要与谷歌相匹配,用户需要连接他们的谷歌帐户。 因此,对话框正在打开: 但是:当点击任何地方而不是对话框时,对话框会消失,并且谷歌适合未连接。
没关系。但我有一个按钮。单击按钮并且谷歌适合未连接时,对话框将再次可见。 但我不知道该怎么做。我已经尝试使用帐户选择器对话框,但谷歌适合也没有连接。
我的代码:
private void buildFitnessClient() {
// Create the Google API Client
client = new GoogleApiClient.Builder(this)
.addApi(AppIndex.API)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_BODY_READ)) //for height and weight
.addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))//for height and weight
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
android.util.Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs. What to do?
// Look at some data!!
isGoogleFitConnected = true;
new read_google_fit_data_task(MainActivity.this, client).execute();
}
@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) {
android.util.Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
android.util.Log.i(TAG, "Connection lost. Reason: Service Disconnected");
}
}
}
)
.enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!authInProgress) {
try {
authInProgress = true;
result.startResolutionForResult(MainActivity.this, ConnectionResult.CANCELED);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.e("GoogleFit", "authInProgress");
}
}
})
.build();
}
然而,当我关闭我的应用并重新启动它时,对话框再次出现,并且可以使用google fit连接。
有人可能知道答案吗?我会很高兴的!