After once login with google authentication in my android app i can't sign out. every time it's automatically sign in

混江龙づ霸主 提交于 2019-12-02 08:56:39

问题


I have integrated Google authenticate login in my app but after once login if I log out my account still every time app automatically sign in the old user account.

MainAcivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    GoogleSignInClient mGoogleSignInClient;
    private FirebaseAuth mAuth;
    private int RC_SIGN_IN=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAuth = FirebaseAuth.getInstance();
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
         mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        if (account != null){
            String personName = account.getDisplayName();
            String personGivenName = account.getGivenName();
            String personFamilyName = account.getFamilyName();
            String personEmail = account.getEmail();
            String personId = account.getId();
            Uri personPhoto = account.getPhotoUrl();
            Intent i=new Intent(MainActivity.this,Welcome.class);
            i.putExtra("pn",personName);           
            i.putExtra("pe",personEmail);         

            startActivity(i);
        }
        findViewById(R.id.sign_in_button).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();
                break;
            // ...
        }
    }
    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);

        // 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);
        }
    }
    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);

            // Signed in successfully, show authenticated UI.
            Intent i=new Intent(MainActivity.this,Welcome.class);
            startActivity(i);
        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Log.w("SignInFailed", "signInResult:failed code=" + e.getStatusCode());
            Toast.makeText(MainActivity.this,"SignInFailed",Toast.LENGTH_SHORT).show();
        }
    }
}

Welcome Acivity

public class Welcome extends AppCompatActivity {
    TextView textView,textView2;
    GoogleSignInClient mGoogleSignInClient;
    private GoogleApiClient mGoogleApiClient;

    private FirebaseAuth mAuth;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        textView=findViewById(R.id.textView);
        textView2=findViewById(R.id.textView2);
        button=findViewById(R.id.button);

        Intent iin= getIntent();
        Bundle b = iin.getExtras();
        if(b!=null)
        {
            String j =(String) b.get("pn");
            textView.setText(j);
            String k =(String) b.get("pe");
            textView2.setText(k);
        }
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        if(account == null){
            Intent i=new Intent(Welcome.this,MainActivity.class);
            startActivity(i);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


               signOut();             

            }
        });

    }
    private void signOut() {
        mGoogleSignInClient.signOut()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Intent i=new Intent(Welcome.this,MainActivity.class);
                        startActivity(i);
                    }
                });
    }
//   
}

I have used google docs about sign out but I can't solve my problem. I haven't found any useful questions from already asked by others. I appreciate any help you folks can offer.


回答1:


You also need to sign out from GoogleSignInClient and FirebaseAuth current user, something like this:

 //sign out of the user and start login activity.
public void signOut() {
    signOutBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();
           GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(getContext(), gso);
           mGoogleSignInClient.signOut();
           FirebaseAuth.getInstance().signOut();
           startActivity(new Intent(getContext(), LoginActivity.class));
        }
    });
}


来源:https://stackoverflow.com/questions/56097948/after-once-login-with-google-authentication-in-my-android-app-i-cant-sign-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!