0
public static final long TIMEOUT = 60000;
public static final long SYSTEM_TIME = System.currentTimeMillis();

I have the TIMEOUT Value for my application set as 60000 and i have my system time. Now how would i know that 50 seconds has been elapsed and i need to show a message to the end-user.

if (TIMEOUT  - SYSTEM_TIME <= 10000) {
    Toast.makeText(getApplicationContext(), "10 Seconds Left", Toast.LENGTH_LONG).show();
    disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}
Kevin
  • 23,174
  • 26
  • 81
  • 111

4 Answers4

1

If you don't need to do other stuff in that thread you can use a sleep(50000).

Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
jamp
  • 2,159
  • 1
  • 17
  • 28
1

This is how to run a specific task one-shot:

new Timer().schedule(new TimerTask() {              
            @Override
            public void run() {
                // TODO
                ...
            }
        }, TIMEOUT);

The doc is here (as reported by jimpanzer)

Alepac
  • 1,833
  • 13
  • 24
1

Maybe that you can use something like this :

long startTime = SystemClock.elapsedRealtime();
// do what you want
long endTime = SystemClock.elapsedRealtime();
long ellapsedTime = endTime - startTime;
if (ellapsedTime>TIME_OUT) {
    // do stuff
}
Fred
  • 38
  • 8
1

Maybe I am wrong or just not had enough coffee yet, but Timeout is 60000, your System value is much more - all millis starting at the year 1970 (if I am not mistaken here as well). This means your result from TIMEOUT - SYSTEM_TIME is negative and therefor a negative number and therefor smaller than 10000. So your if-statement always runs.

Daniel
  • 72
  • 1
  • 2
  • 8