问题
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?
回答1:
I dont have better idea for countdown only calculate on clientside.
What happen if you do it:
- Get Server Time in timestamp (add to
srvTime
variable)
Create new record for "countdown" timer:
- Start time:
srvTime
- 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);
回答2:
Create a server side timer that updates a value in your realtime database.
Firebase ref = new Firebase('/firebase/database/url/time');
new AnimationTimer(){
start(){...}
stop(){...}
handle(){...ref.addValue(time);}
};
That will update your real-time database every millisecond. To see it in your application just call it.
Firebase ref = new Firebase('/firebase/database/url/time/value')
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot ds) {
Platform.runLater(() -> {
label.setText(ds.getValue() + "");
});
}
@Override
public void onCancelled(FirebaseError fe) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
It will update in real-time on your application in milliseconds. I was able to get this running in Java
来源:https://stackoverflow.com/questions/41648186/implementing-a-firebase-server-side-countdown-timer-for-android