mGoogleApiClient无法在另一个活动中实现

时间:2015-11-16 22:29:29

标签: android google-play google-signin

我的项目遇到了问题。 我想使用" Google API"登录我的 LoginActivity 。并从另一个Activity(名为 WelcomeActivity

注销

LoginActivity: (代码为here

public class LoginActivity extends AppCompatActivity implements
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener {

    // Configuration of Google API - Step 1/3
    private static final String TAG = "LoginActivity";
    private static final int RC_SIGN_IN = 9001;
    public static GoogleApiClient mGoogleApiClient;
    private ProgressDialog mProgressDialog;

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

    }
    public void GoogleAPI(){

        // Button listeners
        findViewById(R.id.sign_in_button).setOnClickListener(this);

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

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();
        ....
    }

    // [START onActivityResult]
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }
    // [END onActivityResult]

    // [START handleSignInResult]
    private void handleSignInResult(GoogleSignInResult result) {

        if (result.isSuccess()) {
            // Signed in successfully, show authenticated UI.
            CustomApplication app = (CustomApplication)getApplication();
            GoogleSignInAccount acct = result.getSignInAccount();
            Intent i = new Intent(LoginActivity.this, WelcomePage.class);
            i.putExtra("Username", acct.getDisplayName());
            startActivity(i);
        }
    }
    // [END handleSignInResult]

    // [START signIn]
    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    // [END signIn]

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        ...
    }

    private void showProgressDialog() {
       ...
    }

    private void hideProgressDialog() {
       ...
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();
                break;
            ….
        }
    }

}

我想在欢迎活动中使用Sign_out方法,

private void signOut() {
//        Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
//                new ResultCallback<Status>() {
//                    @Override
//                    public void onResult(Status status) {
//                        // [START_EXCLUDE]
////                        updateUI(false);
//                        // [END_EXCLUDE]
//                    }
//                });
//    }

为了解决这个问题,我尝试了两种方法:

  1. 将mGoogleApiClient作为全局变量(扩展Application或Singleton),我试试,但失败了,在欢迎页面中,mGoogleApiClient不为空,但错误是: mGoogleApiClient尚未连接

  2. 我调用了LoginActivity.getMGoogleApiClient(静态变量),但也失败了,同样的错误: mGoogleApiClient尚未连接

  3. 我已经搜索了这个问题好几天了,但解决它没什么用处,请帮帮我..

2 个答案:

答案 0 :(得分:6)

当你启用自动管理时,googleApiClient会在onStart上连接并在onStop上断开连接,并由库自动处理。您可以在其他活动中注销用户,无论您的代码是从后端注销用户,除了不要从谷歌退出。

伪代码:

private void logOut(){
    //your logout code in the log out activity

    setCurrentUser(null);
}

在使用googleApiClient的活动中,您可以检查用户是否登录了googleApiClient的onConnected回调。如果当前用户未登录,请从Google注销该用户。

@Override
public void onConnected(Bundle connectionHint) {

    if (getCurrentUser() == null){
        signOut();
    }

}

private void signOut() {
    Auth.GoogleSignInApi.signOut(mGoogleApiClientPlus).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    //signed out.
                }
            });
}

如果你没有使用enableAutoManage并且没有断开onStop上的客户端(我不知道它是否推荐),你可以使用你的2种方法,但我赢了&建议对googleApiClient对象使用静态字段。

答案 1 :(得分:1)

一种解决方案是执行正常的注销流程(忽略mGoogleApiClient)。下次用户将您的应用程序打开到登录屏幕并点击谷歌登录时,您先将其注销,然后进行谷歌登录

e.g:

private void googleLogin() {
  googleLogout();
  Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
  startActivityForResult(signInIntent, RC_SIGN_IN);
}

private void googleLogout() {
  if (mGoogleApiClient.isConnected()) {
    Auth.GoogleSignInApi.signOut(mGoogleApiClient);
  }
}