为了账号安全,请及时绑定邮箱和手机立即绑定

Google 登录不会更改活动

Google 登录不会更改活动

郎朗坤 2021-10-28 14:53:30
我刚刚在我的应用程序中实现了 Google 登录,但由于某种原因,当用户单击 google 登录按钮时,它没有激活 MainActivity。这是当用户单击登录按钮时激活的方法:private void signIn() {    Intent signInIntent = mGoogleSignInClient.getSignInIntent();    startActivityForResult(signInIntent, RC_SIGN_IN);}我必须在我想激活的地方添加活动。通常我将它添加到 Intent 中。但是当我这样做时:private void signIn() {    Intent signInIntent = mGoogleSignInClient.getSignInIntent();    signInIntent.setClass(this, MainActivity.class);     startActivityForResult(signInIntent, RC_SIGN_IN);}然后登录不再起作用。
查看完整描述

1 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

正如您在此处看到的,在调用登录意图后,您必须覆盖 onActivityResult 方法来处理它:


@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

  super.onActivityResult(requestCode, resultCode, data);


  // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);

  if (requestCode == RC_SIGN_IN) {

      // The Task returned from this call is always completed, no need to attach

      // a listener.

      Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);

      handleSignInResult(task);

  }

}

然后在您的 handleSignInResult 中,您可以检查登录是否成功,然后开始您的活动


private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {

  try {

      GoogleSignInAccount account = completedTask.getResult(ApiException.class);


      // Signed in successfully, start your activity here.


  } catch (ApiException e) {

      // The ApiException status code indicates the detailed failure reason.

      // Please refer to the GoogleSignInStatusCodes class reference for more information.

      Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());

      updateUI(null);

  }

}


查看完整回答
反对 回复 2021-10-28
  • 1 回答
  • 0 关注
  • 137 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信