要获取已验证用户的信息,首先你需要使用 Firebase Authentication 进行用户身份验证。一旦用户已登录并通过验证,你可以使用 Firebase 实时数据库或云存储来存储和检索用户信息。
下面是一个示例代码,展示了如何使用 Firebase Authentication 获取已验证用户的信息:
首先,确保你已经在 Android 项目中集成了 Firebase Authentication。你可以按照 Firebase 官方文档中的步骤进行设置。
在你的活动或片段中,创建一个 AuthStateListener 对象来监听用户登录状态的更改:
FirebaseAuth mAuth;
// 在 onCreate 方法中初始化 FirebaseAuth 对象
mAuth = FirebaseAuth.getInstance();
// 创建 AuthStateListener 对象
AuthStateListener mAuthListener = new AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// 用户已登录
String userId = user.getUid();
String userEmail = user.getEmail();
// 在这里可以根据需要获取其他用户信息
} else {
// 用户未登录
}
}
};
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
通过以上步骤,你已经可以获取已验证用户的基本信息,如用户的唯一标识符(UID)和电子邮件地址。如果你需要更多的用户信息,你可以在 Firebase 实时数据库或云存储中存储用户的其他属性,并在需要时进行检索。