【问题标题】:What is the best way to keep authentication state保持身份验证状态的最佳方法是什么
【发布时间】:2017-03-09 06:55:49
【问题描述】:

我是 Android 和 Firebase 的新手。我开发了一个课堂应用程序,它有两个方面,即教师和学生,并将数据存储在 Firebase 上。我想在学生端设置警报以提醒他们上课时间。

我有一个AlarmReceiver extends BroadcastReceiver 类来通知学生,并在MainActivity 类上调用它。

我在Application 类上存储了布尔值isStudentisLecturer,也有getter 和setter 方法。当哪个是登录时,设置那个为真。但问题是登录时只有一次它是真的,然后它就全是假的。所以,我想知道如何保持登录状态告诉应用程序“这是学生”或“这是老师”。

附:对不起我的英语不好

【问题讨论】:

标签: android authentication firebase firebase-authentication


【解决方案1】:

为此,您需要使用持久性存储。您可以使用数据库或共享首选项。登录时将学生或讲师的属性设置为真或假(或者您可以存储 0 或 1)并将其存储在存储中(如共享首选项),然后在您终止应用并重新启动应用后从存储中获取该属性并以此为基础采取你的行动。如果您有任何启动画面,您可以测试相同的内容并进行相应的导航。对于共享偏好指南,您可以参考以下链接

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

【讨论】:

    【解决方案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
    }  
    

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 1970-01-01
      • 2015-04-28
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      相关资源
      最近更新 更多