Android Google+登录流程故障

时间:2014-06-13 10:43:34

标签: android login android-activity google-plus

您好我正在尝试在Android上实施简单的Google +登录活动。这是我的主要活动类。我有一个简单的活动布局,有3个按钮。一个Google+登录按钮,一个退出按钮和一个revoke_access按钮。当我第一次在设备上运行应用程序时,我登录,登录按钮消失。但是,当我点击设备上的“退回”按钮并退出应用程序,然后再次点击应用程序图标时,应用程序就会加载,大约1秒后,我仍然可以看到Google+登录按钮,然后它就会消失。我想知道什么是避免用户首先看到这个按钮的最佳方法。如果用户已经登录,则即使是一瞬间,也不会显示Google+登录按钮。感谢。

    public class MainActivity extends Activity implements OnClickListener,
    ConnectionCallbacks, OnConnectionFailedListener {

/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;

/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;

/*
 * A flag indicating that a PendingIntent is in progress and prevents us
 * from starting further intents.
 */
private boolean mIntentInProgress;

private boolean mSignInClicked;
private ConnectionResult mConnectionResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

    setContentView(R.layout.activity_main);

    findViewById(R.id.sign_in_button).setOnClickListener(this);
    findViewById(R.id.btn_logout).setOnClickListener(this);
    findViewById(R.id.btn_revoke_access).setOnClickListener(this);
}

private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
        } catch (IntentSender.SendIntentException e) {
            // The intent was canceled before it was sent. Return to the
            // default
            // state and attempt to connect to get an updated
            // ConnectionResult.
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (!mIntentInProgress) {
        // Store the ConnectionResult so that we can use it later when the
        // user clicks
        // 'sign-in'.
        mConnectionResult = result;

        if (mSignInClicked) {
            // The user has already clicked 'sign-in' so we attempt to
            // resolve all
            // errors until the user is signed in, or they cancel.
            resolveSignInError();
        }
    }
}

@Override
public void onConnected(Bundle connectionHint) {
    // TODO Auto-generated method stub

    // We've resolved any connection errors. mGoogleApiClient can be used to
    // access Google APIs on behalf of the user.

    mSignInClicked = false;
    findViewById(R.id.sign_in_button).setVisibility(View.GONE);
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

}

@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub
    mGoogleApiClient.connect();

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.sign_in_button:
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
        break;

    case R.id.btn_logout:
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
        }
        break;

    case R.id.btn_revoke_access:
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient);
            this.onStart();
        }
        break;
    }
}

protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }

}

protected void onActivityResult(int requestCode, int responseCode,
        Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        if (responseCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

}

0 个答案:

没有答案