Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda