Calculate Duration

MilindaD picture MilindaD · Apr 6, 2011 · Viewed 19.6k times · Source

I have a small android problem, I have a requirement to have a timer to calculate the duration from the time a specific activity was open till a certain button in that activity is clicked, simply how long the activity was open. While googling around I found TimerTask but this seems to run a thread for a certain interval only though and doesent seem ideal for the job from my little Android experience

Any idea on how to calculate the duration? Preferably in a very simple manner

Any help is very welcome

Regards, MilindaD

Answer

WhiteFang34 picture WhiteFang34 · Apr 6, 2011

Just use System.currentTimeMillis() to capture the time when the activity starts and stops. E.g.:

long startTime = System.currentTimeMillis();
// wait for activity here
long endTime = System.currentTimeMillis();
long seconds = (endTime - startTime) / 1000;