【问题标题】:I have an activity in a project but when activity start i have node in firebase it should be online but its not working?我在一个项目中有一个活动,但是当活动开始时,我在 firebase 中有一个节点,它应该在线但它不工作?
【发布时间】:2017-12-31 14:52:48
【问题描述】:

如果我最小化我的应用并运行该活动,它可以工作,但是当我从另一个活动进入该活动时,它就无法工作。

在我的 onCreate 中,如下所示

auth = FirebaseAuth.getInstance();
        currentUser = auth.getCurrentUser();

        db = FirebaseDatabase.getInstance();
        userRef = db.getReference().child("Users").child(auth.getCurrentUser().getUid());

        if(currentUser == null) {
            sendToStart();
        } else {
            userRef.child("online").setValue("true");
        }

我的 onStart 方法中也有相同的内容,但无法正常工作。
你能帮帮我吗?

【问题讨论】:

    标签: android firebase-realtime-database


    【解决方案1】:

    您应该尝试通过在后台服务中执行此语句来更新其节点。

    public class MyService extends Service {
    
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          //TODO do something useful
    
        auth = FirebaseAuth.getInstance();
        currentUser = auth.getCurrentUser();
    
        db = FirebaseDatabase.getInstance();
        userRef = db.getReference().child("Users").child(auth.getCurrentUser().getUid());
    
        if(currentUser == null) {
            sendToStart();
        } else {
            userRef.child("online").setValue("true");
        }
    
    
          return Service.START_NOT_STICKY;
      }
    
      @Override
      public IBinder onBind(Intent intent) {
        //TODO for communication return IBinder implementation
        return null;
      }
    }
    
    
    Intent i= new Intent(context, MyService.class);
    context.startService(i);
    

    【讨论】:

    • 我该怎么做
    • 检查我添加的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    相关资源
    最近更新 更多