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

时间:2017-04-19 09:24:26

标签: android firebase firebase-authentication android-sharedpreferences

让用户登录并获取他们的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();
        } 
    }
};

3 个答案:

答案 0 :(得分:6)

试试这样:

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();
        } 
    }
};

要在其他活动中检索:

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

答案 1 :(得分:3)

试试这个

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();
        } 
    }
};

答案 2 :(得分:0)

使用此代码将您的UID保存到sharedpreference

// for saving uid for shared preference



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();
            SharedPreferences sp = getSharedPreferences(PREFS_UID ,Context.MODE_PRIVATE);
            sp.edit().putInt(Save_UID,userId ).commit();
        } 
    }
};


// Retrieving id to any activity by calling this

SharedPreferences sp = getSharedPreferences(PREFS_UID,Context.MODE_PRIVATE);
int sc  = sp.getInt(Save_UID,0);