gpt4 book ai didi

android - 如何从android上的firebase获取用户uid?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:55 24 4
gpt4 key购买 nike

我想将数据保存到 Firebase。这是我的 Activity 代码:

private Firebase firebaseRef;
Firebase.setAndroidContext(this);

try {
mUserId = firebaseRef.getAuth().getUid();
} catch (Exception e) {
// loginWithMailView();
}
itemsUrl = Constants.FIREBASE_URL + "/Person/" + mUserId +"/Information/";

//Person + null (?) + the other information

这是我的按钮代码:

buttonSave = (Button) findViewById(R.id.btnKaydolKayit);
buttonSave.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
firebaseRef = new Firebase(Constants.FIREBASE_URL);
String name = editTextName.getText().toString().trim();
String surname = editTextSurname.getText().toString().trim();
String gender = final_result.getText().toString();
String mail = editTextMail.getText().toString();
String pass = editTextPass.getText().toString().trim();
String phone = editTextPhone.getText().toString().trim();
String trom = final_resultT.getText().toString();
String bloodGroup = spinnerBlood.getSelectedItem().toString();
String dateBlood = dateTextViewBlood.getText().toString();
String dateTrombo = dateTextViewTrom.getText().toString();

if (pass.isEmpty() || mail.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
firebaseRef.createUser(mail, pass, new Firebase.ResultHandler(){

@Override
public void onSuccess() {
firebaseRef = new Firebase(Constants.FIREBASE_URL);
// firebaseRef.setValue();
AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
builder.setMessage("Hesap başarılı bir şekilde oluşturuldu!").setPositiveButton(R.string.login_button_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(KaydolActivity.this, loginWithMail.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}

@Override
public void onError(FirebaseError firebaseError) {
AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
builder.setMessage(firebaseError.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
});
}

new Firebase(itemsUrl).push().child("name").setValue(name);
new Firebase(itemsUrl).push().child("surname").setValue(surname);
new Firebase(itemsUrl).push().child("gender").setValue(gender);
new Firebase(itemsUrl).push().child("mail").setValue(mail);
new Firebase(itemsUrl).push().child("pass").setValue(pass);
new Firebase(itemsUrl).push().child("phone").setValue(phone);
new Firebase(itemsUrl).push().child("trom").setValue(trom);
new Firebase(itemsUrl).push().child("bloodGroup").setValue(bloodGroup);
new Firebase(itemsUrl).push().child("trom").setValue(trom);
new Firebase(itemsUrl).push().child("dateBlood").setValue(dateBlood);
new Firebase(itemsUrl).push().child("dateTrombo").setValue(dateTrombo);
}
});

我的源代码(用于帮助/例如):https://www.sitepoint.com/creating-a-cloud-backend-for-your-android-app-using-firebase/

当我将数据保存到 firebase 时,如下图所示 ( Login&Auth ) enter image description here

但 mUserId 为空。所以当我查看数据库时 mUserid 为空(如照片) enter image description here

最佳答案

您可以使用这些代码行通过 Android Firebase 框架获取当前用户 Uid

FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
Toast.makeText(this, "" + currentFirebaseUser.getUid(), Toast.LENGTH_SHORT).show();

关于android - 如何从android上的firebase获取用户uid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37566911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com