【问题标题】:What is the best way to keep authentication state保持身份验证状态的最佳方法是什么
【发布时间】:2017-03-09 06:55:49
【问题描述】:
我是 Android 和 Firebase 的新手。我开发了一个课堂应用程序,它有两个方面,即教师和学生,并将数据存储在 Firebase 上。我想在学生端设置警报以提醒他们上课时间。
我有一个AlarmReceiver extends BroadcastReceiver 类来通知学生,并在MainActivity 类上调用它。
我在Application 类上存储了布尔值isStudent 和isLecturer,也有getter 和setter 方法。当哪个是登录时,设置那个为真。但问题是登录时只有一次它是真的,然后它就全是假的。所以,我想知道如何保持登录状态告诉应用程序“这是学生”或“这是老师”。
附:对不起我的英语不好
【问题讨论】:
标签:
android
authentication
firebase
firebase-authentication
【解决方案2】:
试试这个,首先初始化 boolean isStudent = false 和 isLecture = false
当用户登录时,检查您的用户是学生还是讲座。基于那个条件..
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
}