发送数据时,从 Android 应用中的 Firebase 检索 ServerValue.Timestamp
我想知道如何使用 Firebase 的 ServerValue.TIMESTAMP 方法,当我想在 Firebase 服务器上创建时间戳,然后将其检索到本地客户端.
I would like to know how to use Firebase's ServerValue.TIMESTAMP method, when I want to create a timestamp at the Firebase server, and then retrieve it to the local client.
在 Firebase 指南中,只有 javascript 对此案例有更详细的描述,但我很难弄清楚如何将其转换为我的 Android 应用程序.
In Firebase guides, only javascript has a more detailed description of this case, but I'm having a hard time figureing how to translate this in to my Android appliction.
提前致谢!
推荐答案
Firebase.ServerValue.TIMESTAMP
设置为 Map
(包含 {.sv:"timestamp"}
) 告诉 Firebase 用服务器的时间填充该字段.当该数据被读回时,它是实际的 unix 时间戳,它是一个 Long
.
Firebase.ServerValue.TIMESTAMP
is set as a Map
(containing {.sv: "timestamp"}
) which tells Firebase to populate that field with the server's time. When that data is read back, it is the actual unix time stamp which is a Long
.
这样的事情会起作用:
Firebase ref = new Firebase("https://YOUR-FIREBASE.firebaseio.com");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Long timestamp = (Long) snapshot.getValue();
System.out.println(timestamp);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
ref.setValue(ServerValue.TIMESTAMP);
再举一个例子,你可以看看我对这个问题的回答:Android 聊天在 DataSnapshot.getValue() 上崩溃
For another example, you can see my answer to this question: Android chat crashes on DataSnapshot.getValue() for timestamp
相关文章