Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
Android System.currentTimeMillis() value is different on firebase database than generated on app logcat

I am submitting a user last seen on Firebase Realtime Database and before setting value at firebase database I am generating a log with timestamp by System.currentTimeMillis() but when I compare it with logcat and database timestamp it submits a different timestamp on the server.

Look at the logcat it shows the correct timestamp and it should be on the server too

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

Firebase Realtime Database Value of Timestamp enter image description here

1639308009264

Both Values are different that's how I get wrong last seen of the user

1639308715905 - App one 
1639308009264 - Server One

The code I am using for log and to set a value on the server.

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) {

            }
        });


    }



} 

Update: I noticed that the last timestamp (generated by an app) is uploading not the current one, I really don't understand how my last seen is not updating by the current timestamp. Please check gist too for the complete code

I also noticed that on the launch of my activity, the firebase function call twice, first with value boolean connected = dataSnapshot.getValue(Boolean. class); false than true. So with the false value, I am getting the first timestamp which should not update when activity onStop calls and when the app calls onStop it updates the timestamp which was first created on app launch.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

When you call this in your code:

lastOnline.onDisconnect().setValue(offlineTime);

This sends a literal value to the server, with the instruction to write that value when it detects that the client has disconnected. The server has no knowledge of the meaning of the value, and can't make any changes to it.

So if a different value is being written on the server than is being logged in the client, the only reason I can think of is that your calling onDisconnect (elsewhere?) too with a different value.

over 4 years ago · Santiago Trujillo Report

0

System.currentTimeMillis() will get you the epoch time of your emulator , meaning whatever the emulator or the phone system time it will give it to you

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!