2017-03-09 107 views
0

我是Android和Firebase的新手。我開發了一個具有兩面的教室應用程序,它們是教師和學生,並將數據存儲在Firebase上。我想在學生側設置警報以提醒他們的上課時間。什麼是保持身份驗證狀態的最佳方式

我有一個AlarmReceiver extends BroadcastReceiver類通知學生,並呼籲MainActivity類。

我存儲了布爾值isStudentisLecturer上的Application類,也有getter和setter方法。當哪一個登錄時,將其設置爲true。但問題是隻有一次登錄時纔是真的,那麼這一切都是錯誤的。所以,我想知道如何保持登錄狀態以告訴應用程序「這是學生」或「這是老師」。

P.S.對不起,我的英文不好

+0

你可以存儲'isLecturer'和'isStudent'在共享偏好:https://developer.android.com/training/basics/data吸留/共享-preferences.html?HL = ES –

回答

0

爲此,您需要使用持久性存儲。您可以使用數據庫或共享首選項。在登錄時,將學生或講師的屬性設置爲true或false(或者您可以存儲0或1)並將其存儲在存儲區中(如共享首選項),然後一旦你殺了你的應用程序,並重新啓動你的應用程序從存儲中獲取該屬性並基於此採取行動。如果您有任何啓動畫面,您可以測試相同的內容並進行相應導航。對於共享偏好引導您可以參考以下鏈接

https://www.tutorialspoint.com/android/android_shared_preferences.htm

0

試試這個,先初始化布爾isStudent =虛假和isLecture =假 時檢查用戶登錄你的用戶是學生或講座。基於該條件..

mAuthListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
     if(user!=null) 
     { 
      // check user is student or Lecture 
     if(user == student){ 
      // change status of boolean to true 
      boolean isStudent= true 
      //save SP_isStudent = true in sharedprefenence 
     // get status of boolean form sharedPreference 
      if(SP_isStudent == true){ 
      // do your task. 
      } else{ 
      // show toast " user is not currently signin" 
      } 
     } 

    else{ 
     boolean isLecture = true 
    //save SP_isLecture = true in sharedprefenence , 
    // get status of isLecture from shared preference 
     if(SP_isLecture == true{ 
     // do your task. 
     }else{ 
     //show toast " user is not currently signin" 
     } 
    } 
    } 
    } 
}; 

然後在

onResume() { 
    // check status of boolean for isStudent and isLecture 
    //and do your task accordingly 

} 
In onDestroy(){ 
// change boolean to false 
} 
相關問題