【问题标题】:mGoogleApiClient.isConnected returns false even after mGoogleApiClient.connect即使在 mGoogleApiClient.connect 之后,mGoogleApiClient.isConnected 也会返回 false
【发布时间】:2017-10-01 07:51:29
【问题描述】:

我正在使用谷歌登录进行身份验证。

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .requestProfile()
        .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this, this)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

每当用户启动应用程序时,我都会检查会话是否仍然可用。

if (isGoogleSignedIn()) {
    mGoogleApiClient.connect();

在 onConnected 回调后,我将启动下一个活动。

@Override
public void onConnected(@Nullable Bundle bundle) {
    GlobalUtils.googleApiClient = mGoogleApiClient;
    launchNextActivity();
}

我正在 GlobalUtils 中保存对 mGoogleApiClient 的引用,以使用户注销实施更容易。以下是我的 logoutUser 方法。

public static void logOutUser() {

    // Google logout
    if (googleApiClient != null && googleApiClient.isConnected()) {
        Auth.GoogleSignInApi.signOut(googleApiClient);
    }

    googleApiClient = null;
}

但问题是,一旦我离开登录活动,googleApiClient.isConnected() 总是返回 false。我确保在用户进入应用程序主要活动之前始终调用 googleApiClient.connect()。

我做错了什么?

【问题讨论】:

  • 第一个活动不是在其中一个生命周期事件中调用disconnect 对吗?例如在onPauseonStop
  • 不,它不是在调用断开连接。
  • 您确定只创建一个 mGoogleApiClient 实例吗?
  • 是的,我很确定。

标签: android authentication google-signin android-authenticator


【解决方案1】:

我认为问题在于您没有连接 google api 客户端。使用google api时尝试连接,不再使用时断开连接。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    相关资源
    最近更新 更多