Calculate duration

I have a little problem with Android, I have a requirement that the timer calculates the duration from the moment a certain activity is opened until a certain button in this activity is pressed, just how long the activity has been open. While googling around I found TimerTask, but it seems to start the thread at a certain interval only, although it doesent seems ideal for working from my little Android experience.

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

Any help is greatly appreciated.

Regards, MilindaD

+6
java android time duration
source share
2 answers

Just use System.currentTimeMillis() to capture the start and stop times of activity. For example:.

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

If you want to know how long the activity is active.

You can write the appropriate code in

  • onCreate () method and onDestory () method. or

  • onResume () method onPause () method.

depending on your needs.

and use the following code

 long t1 = new Date().getTime(); long t2 = new Date().getTime(); long t3 = t2 - t1; 
0
source share

All Articles