Android firebase创建并显示用户名

时间:2017-04-02 17:22:09

标签: android firebase firebase-realtime-database firebase-authentication

我正在使用带有电子邮件和密码的Firebase用户注册方法。在创建新用户的同时,我还存储了以后需要在应用程序个人资料页面中显示的用户名信息。

如何检索当前登录的用户名?

提前谢谢

enter image description here

private void registration(){

    final String email = Email.getText().toString().toString().trim();
    final String password = Password.getText().toString().trim();
    final String username = Username.getText().toString().trim();
    final String age = Age.getText().toString().trim();
    final String userID = userAuth.getCurrentUser().getUid();

    if (!TextUtils.isEmpty(email)&& !TextUtils.isEmpty(password)&& !TextUtils.isEmpty(username)&& !TextUtils.isEmpty(age)){

        showProgress.setMessage("Registration in progress...");
        showProgress.show();

        userAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if(task.isSuccessful()) {

                    String user_id = userAuth.getCurrentUser().getUid();
                    DatabaseReference current_user_db = DatbaseOfUsers.child(user_id);
                    current_user_db.child("email").setValue(email);
                    current_user_db.child("username").setValue(username);
                    current_user_db.child("Age").setValue(age);
                    current_user_db.child("uID").setValue(userID);

                    showProgress.dismiss();

                    //After user is created main screen intent is called
                    Intent mainpage = new Intent(RegisterActivity.this, MainPageActivity.class);
                    mainpage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(mainpage);
                }

                else if(!task.isSuccessful()){
                    showProgress.dismiss();
                    Toast.makeText(RegisterActivity.this,"Error While Register",Toast.LENGTH_LONG).show();
                }
            }

        });

    }

    else{
        showProgress.dismiss();
        Toast.makeText(RegisterActivity.this,"Please Enter All Required Fields",Toast.LENGTH_LONG).show();
    }

2 个答案:

答案 0 :(得分:0)

private FirebaseAuth mAuth;
    mAuth = FirebaseAuth.getInstance();
    final FirebaseUser Theuser = mAuth.getCurrentUser();
      if (Theuser !=null)
    _UID = Theuser.getUid();

答案 1 :(得分:0)

您可以向(function($) { $(document).ready(function() { setTimeout( function() { $(".active .owl-video-play-icon").trigger("click"); }, 1000); }); $(document).on('click', '.owl-dot', function() { if ($('.owl-item.active').hasClass('owl-video-playing')) {} else { setTimeout( function() { $(".active .owl-video-play-icon").trigger("click"); }, 1000); } }); })(jQuery); 用当前用户的用户发送请求。

Users

FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser(); DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(); mDatabase.child("Users").orderByChild(currentUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { } @Override public void onCancelled(DatabaseError databaseError) { } }); 返回值,您看起来为onDataChange。然后将它投射到您的模型中,获得您想要的任何想法。