开发者社区> 问答> 正文

Firebase注销Google

我是一个初学者程序员,但是有一个问题。如何在“活动”菜单中使用Google注销功能?我在这个问题上坐了很长时间,以某种方式我无法解决它。活动菜单用于显示用户信息。

主要活动

public class MainActivity extends AppCompatActivity {
    SignInButton signInButton;
    public GoogleSignInClient mGoogleSignInClient;
    private final static int RC_SIGN_IN = 123;
    private FirebaseAuth mAuth;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mAuth = FirebaseAuth.getInstance();
        signInButton=(SignInButton)findViewById(R.id.sign_in_button);


        createRequest();


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


    }


    @Override
    protected void onStart() {
        super.onStart();


        FirebaseUser user = mAuth.getCurrentUser();
        if(user!=null){
            Intent intent = new Intent(getApplicationContext(),MenuActivity.class);
            startActivity(intent);
        }


    }


    private void createRequest() {


        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();


        // Build a GoogleSignInClient with the options specified by gso.
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);


    }


    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 GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            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
                // ...
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }


    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {


        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.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
                            FirebaseUser user = mAuth.getCurrentUser();
                            Intent intent = new Intent(getApplicationContext(),MenuActivity.class);
                            startActivity(intent);


                        } else {
                            Toast.makeText(MainActivity.this, "Sorry auth failed.", Toast.LENGTH_SHORT).show();

                        }


                        // ...
                    }
                });
    }

}

菜单活动

logoutBtn是一个按钮,单击后将用于从Google注销用户并将其咀嚼到MainActivity,以便他可以再次选择要登录到我的应用程序的帐户

Button logoutBtn;
    TextView userName;
    ImageView profileImage;
    ImageButton play_button, multi_button, statistic_button, option_button;

    FirebaseAuth mAuth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        logoutBtn = (Button) findViewById(R.id.logoutBtn);
        userName = (TextView) findViewById(R.id.name);
        profileImage = (ImageView) findViewById(R.id.profileImage);


        GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
        if(signInAccount != null) {
            userName.setText(signInAccount.getDisplayName());
            //mail.setText(signInAccount.getEmail());

        }



        logoutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


        });
    }'

问题来源:Stack Overflow

展开
收起
montos 2020-03-27 22:55:12 588 0
1 条回答
写回答
取消 提交回答
  • 您可以方便地使用它进行注销(详细信息在这里https://firebase.google.com/docs/auth/android/firebaseui):

    AuthUI.getInstance()
            .signOut(this)
            .addOnCompleteListener {
                // ...
            }
    

    对于片段,“ this”必须替换为requireContext()。 回答来源:Stack Overflow

    2020-03-27 22:55:36
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
TensorFrames: Google Tensorflo 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载