猿问

Google 登录不会更改活动

我刚刚在我的应用程序中实现了 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);

}

然后登录不再起作用。


郎朗坤
浏览 138回答 1
1回答

温温酱

正如您在此处看到的,在调用登录意图后,您必须覆盖 onActivityResult 方法来处理它:@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);&nbsp; if (requestCode == RC_SIGN_IN) {&nbsp; &nbsp; &nbsp; // The Task returned from this call is always completed, no need to attach&nbsp; &nbsp; &nbsp; // a listener.&nbsp; &nbsp; &nbsp; Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);&nbsp; &nbsp; &nbsp; handleSignInResult(task);&nbsp; }}然后在您的 handleSignInResult 中,您可以检查登录是否成功,然后开始您的活动private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {&nbsp; try {&nbsp; &nbsp; &nbsp; GoogleSignInAccount account = completedTask.getResult(ApiException.class);&nbsp; &nbsp; &nbsp; // Signed in successfully, start your activity here.&nbsp; } catch (ApiException e) {&nbsp; &nbsp; &nbsp; // The ApiException status code indicates the detailed failure reason.&nbsp; &nbsp; &nbsp; // Please refer to the GoogleSignInStatusCodes class reference for more information.&nbsp; &nbsp; &nbsp; Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());&nbsp; &nbsp; &nbsp; updateUI(null);&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答