且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为Android实现Firebase服务器端倒数计时器?

更新时间:2023-11-27 21:48:10

我没有更好的主意倒计时只计算在客户端。 / p>

如果你这样做,会发生什么情况:
$ b


  1. 获取服务器时间的时间戳到 srvTime 变量)

为倒计时计时器创建新记录:


  1. 开始时间: srvTime

  2. 时间:倒计时结束时的时间(ms)(5min = 60000 * 5)



您可以在客户端进行计算。

ServerValue.TIMESTAMP创建一个侦听器

code

$ p $ ref.addValueEventListener(new ValueEventListener(){
publ ic void onDataChange(DataSnapshot dataSnapshot){
System.out.println(dataSnapshot.getValue());
}

public void onCancelled(DatabaseError databaseError){}
});
ref.setValue(ServerValue.TIMESTAMP);


Is there a way to implement a Firebase server-side countdown timer in Android Studio?

I want the timer to be server side, meaning that whenever a user opens my app the counter will always be at the same time for all users.

I read the answers to this question, but they're 2+ years old and Firebase changed a lot since. It was acquired by Google in October 2014 and a significant number of new features were featured in May 2016 at Google I/O.

If Firebase can't provide that feature, is there another platform that can provide server-side countdown?

I dont have better idea for countdown only calculate on clientside.

What happen if you do it:

  1. Get Server Time in timestamp (add to srvTime variable)

Create new record for "countdown" timer:

  1. Start time: srvTime
  2. End time: Time in ms when countdown ended for example (5min = 60000*5)

And you can calculate in client side.

Or create a listener, for ServerValue.TIMESTAMP

ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println(dataSnapshot.getValue()); 
    }

    public void onCancelled(DatabaseError databaseError) { }
});
ref.setValue(ServerValue.TIMESTAMP);