简体   繁体   English

Firebase / Android:获取当前用户电子邮件

[英]Firebase/Android: Get current user email

I am trying to get the email address of the current user. 我正在尝试获取当前用户的电子邮件地址。 I have the following code in my onCreate, but it isn't working: 我的onCreate中包含以下代码,但无法正常工作:

FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if (firebaseUser != null) {
            userEmail = firebaseUser.getEmail();
        }
    }
};

Note that variable authListener at the top is "never used" according to my program. 请注意,根据我的程序,顶部的authListener变量是“从未使用过”。 Not sure if this has anything to do with it. 不知道这是否与它有关。 Does anyone know what could be wrong with the code I have above? 有人知道我上面的代码有什么问题吗?

Update, got it working! 更新,使其正常工作! The below code gets the current users email. 以下代码获取当前用户的电子邮件。

            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if (user != null) {
                String userEmail = user.getEmail();
            } else {
                // No user is signed in
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM