【问题标题】:How to use firebase database from broadcastreceiver如何从广播接收器使用firebase数据库
【发布时间】:2017-05-06 11:31:20
【问题描述】:

我正在尝试使用来自 BroadcastReceiver 的 Firebase 数据库来获取一些数据。不幸的是,用户有时是null,这意味着我不能使用 UID 来获取数据。

@Override
public void onReceive(final Context context, Intent intent) { 
    FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
                    if(user != null){
                        //sometimes not null
                    }else{
                        //sometimes null
                    }
                }
     });
}

FirebaseAuth.getInstance().getCurrentUser 有时也是null。另外,我没有在我的broadcastreceiver 中运行很长时间的操作。我说的是获取一个没有任何节点的单个 opjes。

如何从BroadcastReceiver 使用 Firebase 数据库?

【问题讨论】:

    标签: android firebase-realtime-database firebase-authentication


    【解决方案1】:

    尝试委托像这样的意图服务

    public class AuthenticationCheckService extends IntentService {
    
    public AuthenticationCheckService() {
        super("AuthenticationCheckService");
    }
    
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        //Once this code runs and its done the service stops so need to worry about long running service
        checkAuthenticationStatus();
    }
    
    private void checkAuthenticationStatus() {
        FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth)  {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                //Do stuffs
            } else {
                //Resolve to authentication activity
            }
        }
    });
    }
    }
    

    然后像这样在onReceive方法里面启动服务

    @Override
    public void onReceive(final Context context, Intent intent) { 
     //Method implemented below  
     checkAuthStatus(context);
    
    }
    
    private void checkAuthStatus(Context context) {
        Intent authCheckIntent = new Intent(context, AuthenticationCheckService.class);
        context.startService(authCheckIntent);
    }
    

    然后记得在清单文件中注册服务

     <service android:name=".AuthenticationCheckService" />
    

    【讨论】:

    【解决方案2】:

    BroadcastReceiver 不适合用于长时间运行的操作,请尝试从该接收器启动 IntentService。如果工作时间过长,Android 将完成此接收器。也许,这就是结果有时为空的原因。

    【讨论】:

    • 感谢您的回复。不幸的是,这不是一个长期运行的操作。我说的是基于 id 获取单个项目。此项目下还有一个无节点。
    • 此外,这是一个回调。我正在向stateChangListener 添加一个对象。这意味着其中的代码不会在broadcastReceiver 中执行
    • 但是你传递了接收者的上下文 - 接收者可能已关闭。你可以尝试阻塞 IntentService 的线程直到回调被触发
    • 使用 CountDownLatch - 得到一些结果后,保存并恢复服务线程。
    【解决方案3】:

    确认您的用户/设备是否已登录/登录到 Firebase 有关如何验证用户身份的更多信息,请查看 Set up Firebase Authentication for Android

    【讨论】:

    • 我不是在问如何使用 firebase。正如我的问题中所述,我已经启动并运行了它。我只是遇到了这个特定的用例。
    • 如果用户没有被认证,FirebaseUser user = firebaseAuth.getCurrentUser(); 会一直返回null
    【解决方案4】:

    我想我明白了。问题不是广播接收器,而是firebase刷新令牌。那时用户是null。这样的事情奏效了:

    @Override
    public void onReceive(final Context context, Intent intent) { 
        FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
                    if(user != null){
                        FirebaseAuth.getInstance().removeAuthStateListener(this);
                        //do stuff
                    }else{
                        //do nothing
                    }
                }
         });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多