Google注销问题

时间:2019-01-19 10:32:41

标签: android android-studio

我一直在创建一个具有注册功能并使用gmail帐户登录的登录应用程序。 问题是,每当我对其帐户签名时,该帐户就会自动使用第一个帐户登录,并且无法注销。 在Firebase中,显示未登录的用户ID。 每次对同一个帐户进行签名。

这是我的个人资料活动。 在用户登录个人资料活动后打开。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    auth=FirebaseAuth.getInstance();
    profiletx=(TextView)findViewById(R.id.ptext);
    user=auth.getCurrentUser();
    profiletx.setText(user.getEmail());
    signout=(Button)findViewById(R.id.signout);

}
 public void signOut(View v)
{
    FirebaseAuth.getInstance().signOut();
    auth.signOut();
    Intent i=new Intent(getApplicationContext(), MainActivity.class);
    finish();
    startActivity(i);

}

这是我的LOginActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    auth = FirebaseAuth.getInstance();
    e1 = (EditText) findViewById(R.id.editText);
    e2 = (EditText) findViewById(R.id.editText2);
    user = auth.getCurrentUser();
    signInButton = (SignInButton) findViewById(R.id.googleid);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SignIn();

        }
    });

}
private void SignIn()
{
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, 1);
}
@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 == 1) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);
            firebaseAuthWithGoogle(account);
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
            Log.w(TAG, "Google sign in failed", e);

            // ...
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
    AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
    auth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d("Login", "signInWithCredential:success");
                        user = auth.getCurrentUser();
                        updateUI(user);
                        FirebaseUser user = auth.getCurrentUser();
                        Toast.makeText(getApplicationContext(), "successfully signed", Toast.LENGTH_SHORT).show();
                        finish();
                        Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
                        startActivity(i);

                    } else {
                        Toast.makeText(getApplicationContext(), "unsuccessfully sign", Toast.LENGTH_SHORT).show();
                        updateUI(null);

                    }

                    // ...
                }
            });
}
private void updateUI(FirebaseUser user) {
    GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
    if (acct != null) {
        String personName = acct.getDisplayName();
        String personGivenName = acct.getGivenName();
        String personFamilyName = acct.getFamilyName();
        String personEmail = acct.getEmail();
        String personId = acct.getId();
        Uri personPhoto = acct.getPhotoUrl();
    }
}


    public void Signin(View v)
    {
        if (e1.getText().toString().equals("") && e2.getText().toString().equals("")) {
            Toast.makeText(getApplicationContext(), "enter the details please", Toast.LENGTH_SHORT).show();

        } else {
            auth.signInWithEmailAndPassword(e1.getText().toString(), e2.getText().toString())
                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(getApplicationContext(), "successfully registered", Toast.LENGTH_SHORT).show();
                                finish();
                                Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
                                startActivity(i);
                            } else {
                                Toast.makeText(getApplicationContext(), "could not logiged in", Toast.LENGTH_SHORT).show();
                            }


                        }
                    });
        }
    }
}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您还需要注销GoogleSignInClient,例如单击按钮时:

books/:id

}