第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

android在設備上保存非常小的數(shù)據(jù)的最佳方式

android在設備上保存非常小的數(shù)據(jù)的最佳方式

蕪湖不蕪 2021-09-03 13:23:37
在我的應用程序啟動活動中,每次啟動應用程序時我都需要檢查三件事應用版本是用戶登錄是否創(chuàng)建了用戶帳戶我使用Firebase作為數(shù)據(jù)庫,所以每次使用啟動應用程序時,我都會在Datachange上檢查數(shù)據(jù)庫,然后根據(jù)返回結(jié)果和案例區(qū)域?qū)⒂脩舭l(fā)送到活動,如下所示://check if newer version is available (Step 1)if (appVersionMatch) {    CheckLogin();} else {    //Take user to appstore for app update}// (Step 2)public void CheckLogin() {    if (userLogin) {        CheckUserExist()    } else {        //Show user Login activity    }}// (Step 3)public void CheckUserExist() {    if (user.exist()) {        //Go To main Activity    } else {        //Go To Register activity    }}這個流程工作正常,但檢查所有這三件事總是需要一些時間。我嘗試使用以下方法更快但沒有按預期工作: SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);                    editor = pref.edit();                    boolean isLoogenIn = pref.getBoolean("userLoginCheck", false);
查看完整描述

2 回答

?
楊魅力

TA貢獻1811條經(jīng)驗 獲得超6個贊

這是我使用SharedPreferences.


首先創(chuàng)建一個單獨的類(我用它來保存其他信息,如 url、常量等)在其中創(chuàng)建一個SharedPreferences.


public class project_constants {

private static String PREF_NAME = "project_pref";


private static SharedPreferences getPrefs(Context context) {

    return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

}


public static boolean getUserLogin(Context context) {

    return getPrefs(context).getBoolean("login", false);

}


public static void setUserLogin(Context context, boolean input) {

    SharedPreferences.Editor editor = getPrefs(context).edit();

    editor.putBoolean("login", input);

    editor.apply();

}

現(xiàn)在,當用戶登錄時,您應該使用project_constants.setuserLogin(getApplicationContext,True);.


現(xiàn)在,當您要檢查用戶是否已登錄時,可以使用project_constants.getuserLogin(getApplicationContext);,如果是,則用戶已登錄,否則為否。


查看完整回答
反對 回復 2021-09-03
?
不負相思意

TA貢獻1777條經(jīng)驗 獲得超10個贊

第一次,當數(shù)據(jù)從 firebase 準備好時,您應該將數(shù)據(jù)保存在 SharedPreference 中:


SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);

editor = pref.edit();

editor.putBoolean("userLoginCheck", false);

editor.commit();

然后您可以通過以下方式獲得下次的偏好值:


 boolean isLoogenIn = pref.getBoolean("userLoginCheck", true);


查看完整回答
反對 回復 2021-09-03
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號