gpt4 book ai didi

Android-如何将 Firebase Uid 保存到 SharedPreferences?

转载 作者:太空狗 更新时间:2023-10-29 15:44:38 24 4
gpt4 key购买 nike

在让用户登录并获得他们的 uid 后,我如何将他们的 uid 保存到 SharedPreferences 以便我可以在我的整个项目中访问它?这是我获取他们的 uid 的代码:

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

最佳答案

像这样尝试:

private SharedPreferences sharedPref;
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
String userId = firebaseUser.getUid();
String userEmail = firebaseUser.getEmail();
sharedPref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("firebasekey", userId);
editor.commit();
}
}
};

在其他 Activity 中检索:

        sharedPref = getPreferences(MODE_PRIVATE);
String UserId = sharedPref.getString("firebasekey", "");

关于Android-如何将 Firebase Uid 保存到 SharedPreferences?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43491554/

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