gpt4 book ai didi

java - Android System.currentTimeMillis() 值在 firebase 数据库上与在应用程序 logcat 上生成的不同

转载 作者:行者123 更新时间:2023-12-05 00:00:47 25 4
gpt4 key购买 nike

我正在提交一个在 Firebase 实时数据库上最后一次看到的用户,在 firebase 数据库设置值之前,我正在通过 System.currentTimeMillis() 生成一个带有时间戳的日志,但是当我将它与 logcat 和数据库进行比较时timestamp 它在服务器上提交一个不同的时间戳。

查看 logcat,它显示了正确的时间戳,它也应该在服务器上

Disconnect with the firebase server 
Offline Time 5:01 pm 1639308715905

时间戳的 Firebase 实时数据库值 enter image description here

1639308009264

这两个值不同,这就是我最后一次看到用户时出错的原因

1639308715905 - App one 
1639308009264 - Server One

我用于记录和在服务器上设置值的代码。

public class MainActivity extends AppCompatActivity {

public static final String TAG = "Main Activity";

FirebaseDatabase DatabaseInstance;
DatabaseReference infoConnected;




@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "On Resume Running");
DatabaseInstance.goOnline();

}


@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "ON STOP CALLING");
DatabaseInstance.goOffline();


}


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseInstance = FirebaseDatabase.getInstance(); // Single Instance
infoConnected = DatabaseInstance.getReference(".info/connected"); // To check firebase connection state
initialiseOnlineStatus();

setContentView(R.layout.main);
}





private void initialiseOnlineStatus() {
final DatabaseReference Online = DatabaseInstance.getReference("Users/" + userID + "/Online");
final DatabaseReference lastOnline = DatabaseInstance.getReference("Users/" + userID + "/lastSeen");


infoConnected.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
boolean connected = dataSnapshot.getValue(Boolean.class);

if (connected) {
Online.setValue(Boolean.TRUE);
lastOnline.removeValue();

Log.d(TAG, "Connected To Firebase Server ");
} else {
Log.d(TAG, "Disconnect with firebase server ");
Online.onDisconnect().setValue(Boolean.FALSE);
String offlineTime = String.valueOf(System.currentTimeMillis());
Log.d(TAG, "Offline Time " + App_Functions.getLocalDeviceTimestamp(Long.parseLong(offlineTime)) + " " + offlineTime); // Get local device time from milliseconds
lastOnline.onDisconnect().setValue(offlineTime);

}

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});


}



}

更新:我注意到上传的最后一个时间戳(由应用程序生成)不是当前时间戳,我真的不明白我最后一次看到的时间戳为什么没有更新到当前时间戳。请查看gist完整代码也是如此

我还注意到,在我的 Activity 启动时,firebase 函数调用了两次,第一次调用值 boolean connected = dataSnapshot.getValue(Boolean.class); false 而不是 true。因此,使用 false 值,我得到第一个时间戳,当 Activity onStop 调用时不应更新,当应用程序调用 onStop 时,它会更新首先在应用程序上创建的时间戳发射。

最佳答案

我要感谢 @argzdev@frank在 github 上帮助我。我通过@argzdev 从 Github 获得的解决方案:

The reason this doesn't work is because you're tryingto set a value after you've already disconnected the connection, it'sbound to cause issues.

What you can do is change the logic of how you store the timestamp.Simply store the timestamp BEFORE you sever the connection. Thereshouldn't be any difference in time, even if there was, it would onlybe miniscule.

Relevant code:

 @Override
> protected void onResume() {
> super.onResume();
> Log.d(TAG, "On Resume Running");
>
> Online.setValue(Boolean.TRUE);
> lastOnline.removeValue();
> DatabaseInstance.goOnline();
> }
>
> @Override
> protected void onStop() {
> super.onStop();
> Log.d(TAG, "ON STOP CALLING");
>
> Online.setValue(Boolean.FALSE);
> lastOnline.setValue("" + System.currentTimeMillis());
> DatabaseInstance.goOffline();
> }
>

Then you can simply remove the onDisconnect in yourValueEventListener.

关于java - Android System.currentTimeMillis() 值在 firebase 数据库上与在应用程序 logcat 上生成的不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70323201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com