Android 保持身份验证状态的最佳方法是什么
我是Android和Firebase的新手。我开发了一个课堂应用程序,它有两个方面,即教师和学生,并将数据存储在Firebase上。我想在学生端设置闹钟,提醒他们上课时间 我有一个Android 保持身份验证状态的最佳方法是什么,android,authentication,firebase,firebase-authentication,Android,Authentication,Firebase,Firebase Authentication,我是Android和Firebase的新手。我开发了一个课堂应用程序,它有两个方面,即教师和学生,并将数据存储在Firebase上。我想在学生端设置闹钟,提醒他们上课时间 我有一个AlarmReceiver类通知学生,并在MainActivity类中调用它 我在应用程序类上存储了booleanisStudent和isarchitector,还有getter和setter方法。当哪个是登录时,将其设置为true。但问题是,登录时只有一次是真的,然后全部是假的。所以,我想知道如何保持登录状态来告诉应
AlarmReceiver
类通知学生,并在MainActivity
类中调用它
我在应用程序
类上存储了booleanisStudent
和isarchitector
,还有getter和setter方法。当哪个是登录时,将其设置为true。但问题是,登录时只有一次是真的,然后全部是假的。所以,我想知道如何保持登录状态来告诉应用程序“这是学生”或“这是老师”
另外,很抱歉我的英语不好为此,您需要使用持久性存储。您可以使用数据库或共享首选项。登录时,将学生或讲师的属性设置为true或false(或者您可以存储0或1),并将其存储在存储器中(如共享首选项),然后在您关闭应用程序并重新启动应用程序后,从存储器中获取该属性并根据该属性采取行动。如果你有任何启动屏幕,你可以测试同样的东西,并相应地导航。有关共享首选项指南,请参阅下面的链接
尝试此操作,首先初始化布尔值isStudent=false和IsArchitecture=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
}
您可以在共享首选项中存储
isArchitector
和isStudent
: