用户在创建用户Android时的Firebase setDisplayName [英] Firebase setDisplayName of user while creating user Android

查看:101
本文介绍了用户在创建用户Android时的Firebase setDisplayName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建用户时,我想能够设置他/她的显示名称。我如何在Android中做到这一点?这里是我想要实现的一个例子:
$ b $ pre $ code> mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this,new OnCompleteListener< (@NonNull Task< AuthResult>任务){
if(task.isSuccessful()){
FirebaseUser.getCurrentUser() ).setDisplayName(mName); //我想这样做
}
});

假设所有的变量都被正确的声明和/或初始化了。 div class =h2_lin>解决方案

我在Firebase文档中找到了答案。我将在这里引用它:如果登录成功,则AuthStateListener运行onAuthStateChanged回调。在回调中,可以使用getCurrentUser方法获取用户的帐户数据。以下是链接: https://firebase.google.com/docs/ auth / android / password-auth#sign_in_a_user_with_an_email_address_and_password

这意味着如果你做了上面的代码(减去FirebaseUser行),然后声明并初始化Firebase AuthStateListener(如下所示),您可以设置用户的显示名称,然后转到您想要的任何其他活动:

  mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!= null){
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(mName).build();
user.updateProfile(profileUpdates);
Intent intent = new Intent(currentActivity.this,nextActivity.class);
startActivity(intent);
}
}
};

不要忘记在onResume()中添加AuthStateListener,如下所示:

  @Override 
public void onResume(){
super.onResume();
mAuth.addAuthStateListener(mAuthListener);

$ / code>

同样,不要忘记在onStop方法中移除它,如下所示:

  @Override 
public void onStop(){
super.onStop();
if(mAuthListener!= null){
mAuth.removeAuthStateListener(mAuthListener);
}
}

完成!您可以设置用户的显示名称,以便在其他活动中使用它。如果您想迎接用户或访问与显示名称绑定的任何其他用户数据,这将非常有用。


When creating a user, I want to be able to set his/her display name. How do I do this in Android? Here is an example of what I want to achieve:

mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful()){
                FirebaseUser.getCurrentUser().setDisplayName(mName); //I want to do this
            }
});

Assume all variables have been declared and/or initialized correctly.

解决方案

I found the answer in the Firebase docs. I will quote it here: "If sign-in succeeded, the AuthStateListener runs the onAuthStateChanged callback. In the callback, you can use the getCurrentUser method to get the user's account data." Here is the link: https://firebase.google.com/docs/auth/android/password-auth#sign_in_a_user_with_an_email_address_and_password

So that means, if you do the above code (minus the FirebaseUser line), and then declare and initialize a Firebase AuthStateListener like shown below, you can set the user's display name and then move on to any other activity you want:

mAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if(user!=null){
            UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                        .setDisplayName(mName).build();
            user.updateProfile(profileUpdates);
            Intent intent = new Intent(currentActivity.this, nextActivity.class);
            startActivity(intent);
        }
    }
};

And don't forget to add the AuthStateListener in onResume() like so:

@Override
public void onResume(){
    super.onResume();
    mAuth.addAuthStateListener(mAuthListener);
}

Likewise, don't forget to remove it in the onStop method like so:

@Override
public void onStop(){
    super.onStop();
    if(mAuthListener != null){
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

And done! You set the user's display name so you can use it in other activities. This would be useful if you want to greet the user or access any other user data tied to the display name.

这篇关于用户在创建用户Android时的Firebase setDisplayName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆