【问题标题】:how to disconnect from googleapiclient in android如何在android中断开与googleapiclient的连接
【发布时间】:2015-04-10 09:17:26
【问题描述】:
我正在使用 API 进行简单的谷歌登录和注销。但是当我使用下面的代码注销时,有时 googleapiclient 不会断开连接。因此,当我要登录时,它会自动登录到我以前的帐户,而不是要求选择帐户。
注销代码:
public void googlePlusLogout() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
Log.e("APPLICATION", "DISCONNECTED");
}
当我调用上述方法进行注销时,有时mGoogleApiClient.disconnect() 不会被调用。帮我解决这个问题。
【问题讨论】:
标签:
android
google-plus
android-googleapiclient
【解决方案1】:
有一个新的 API 试试这个:
@Override
public void logOut() {
if (googleApiClient.isConnected()) {
signOut();
revokeAccess();
googleApiClient.disconnect();
}
}
private void signOut() {
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
App.logger().d(TAG, "signOut: status = " + status);
}
});
}
private void revokeAccess() {
Auth.GoogleSignInApi.revokeAccess(googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
App.logger().d(TAG, "revokeAccess: status = " + status);
}
});
}
【解决方案2】:
在多次内存泄漏之后,这就是我想出的。没有更多的内存泄漏。它很冗长吗?是的。是不是太冗长了?可能。它有效吗?是的。
if (mGoogleApiClient !=null) if (mGoogleApiClient.isConnected()) LocationServices.FusedLocationApi.removeLocationUpdates(this);
if (mGoogleApiClient !=null) mGoogleApiClient.unregisterConnectionCallbacks(this);
if (mGoogleApiClient !=null) mGoogleApiClient.unregisterConnectionFailedListener(this);
if (mGoogleApiClient !=null) if (mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect();
mGoogleApiClient =null;