无法通过Google

时间:2017-12-29 20:37:22

标签: java android google-signin

我无法通过谷歌打开登录活动。这是我的应用代码。

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

            mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    SignInButton signInButton = findViewById(R.id.sign_in_button);
            signInButton.setOnClickListener(view -> signIn());

private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            account = completedTask.getResult(ApiException.class);
        } catch (ApiException e) {
            Log.w("Err", "signInResult:failed code=" + e.getStatusCode());
        }
    }

我尝试使用不同的RC_SIGN_IN代码,例如901,1但仍然没有解决方案。

2 个答案:

答案 0 :(得分:0)

你必须使用代码,

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

mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
         .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
         .build();

signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                startActivityForResult(signInIntent, RC_SIGN_IN);
            }
        });

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);
        }
    }  

handleSigninResult,

private void handleSignInResult(GoogleSignInResult result) {
       Log.d(TAG, "handleSignInResult:" + result.isSuccess());
       if (result.isSuccess()) {
           // Signed in successfolly, show authenticated UI.
       } 
   }

答案 1 :(得分:0)

尝试此链接以xml格式制作布局,并定义按钮以单击它以进行Google登录。在您的活动中设置登录按钮,并在AndroidManifest.xml中以启动器或启动时提及此活动

Google sign in using firebase working on emulator but not on real device? 使用xml中的login.java活动和按钮设计将这些活动与xml链接起来。 还请提及您在Google登录后要移动的活动