简体   繁体   中英

Why doesn't my GoogleApiClient work after I log in one time?

I've implemented a GoogleApiClient for Google Plus sign in and it works great the first time around. If I head back to the log in Activity afterwards though, the Activity will just finish on its own. Sometimes it'll take a moment, but most of the time it's right away. I noticed that when I just don't call GoogleApiClient.connect in Activity.onStart or comment out GoogleApiClient.Builder.addConnectionCallbacks , then there is no crash, but obviously I can't use G+ sign in anymore.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, Plus.PlusOptions.builder().build())
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    if (mApiClient != null && !mApiClient.isConnecting()) {
        mApiClient.connect();
    }
}

@Override
protected void onStop() {
    super.onStop();
    if (mApiClient != null && mApiClient.isConnected()) {
        mApiClient.disconnect();
        mApiClient.unregisterConnectionCallbacks(this);
        mApiClient.unregisterConnectionFailedListener(this);
        mApiClient = null;
    }
}

My callbacks are standard as well.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RC_SIGN_IN:
                mSignInProgress = resultCode == RESULT_OK ? STATE_SIGN_IN : STATE_DEFAULT;
                if (mApiClient != null && !mApiClient.isConnecting()) {
                    mApiClient.connect();
                }
                break;
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        mSignInProgress = STATE_DEFAULT;
        ...
    }

    @Override
    public void onConnectionSuspended(int i) {
        if (mApiClient != null && !mApiClient.isConnecting()) {
            mApiClient.connect();
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (mSignInProgress != STATE_IN_PROGRESS) {
            mSignInIntent = connectionResult.getResolution();
            if (mSignInProgress == STATE_SIGN_IN) resolveLogin();
        }
        if (mApiClient != null) {
            mApiClient.disconnect();
        }
    }

private void resolveLogin() {
    if (mSignInIntent == null) return;
    try {
        mSignInProgress = STATE_IN_PROGRESS;
        startIntentSenderForResult(mSignInIntent.getIntentSender(), RC_SIGN_IN, null, 0, 0, 0);
    } catch (IntentSender.SendIntentException e) {
        mSignInProgress = STATE_SIGN_IN;
        if (mApiClient != null && !mApiClient.isConnecting()) {
            mApiClient.connect();
        }
    }
}

Everything I've done is largely based off of Google's example . So, just be clear. I log in once and as is well. Afterwards, if I start that Activity again, it will just Activity.finish after a very brief moment.

你有没有想过使用BaseGameUtil,这是很容易设置和会照顾你所有的日志记录和输出,可选登入/注销按钮,您可以通过以下设置了这个 。我最近设置这和它被证明是非常有用的功能集。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM