Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - To measure the time between the two button clicks

I have a button named Check In. My aim is on click to change the text and start counting the time. The timer has to stop at the next click. It should give the time taken between the two button clicks.

like image 674
siraj Avatar asked Nov 01 '11 10:11

siraj


2 Answers

On the first click create a variable:

long startTime = System.currentTimeMillis();

Then on the second click you can calculate the difference:

long difference = System.currentTimeMillis() - startTime;

difference / 1000 will give you the difference in seconds. Hope this helps.

like image 115
Egor Avatar answered Nov 13 '22 05:11

Egor


I think it is better practice to use System.nanoTime() instead of System.currentTimeMillis(), as currentTimeMillis() relies on what time the system's clock is set to, which can be changed.

nanoTime(), however, is really designed to measure elapsed time according to the JavaDoc.

like image 25
personne3000 Avatar answered Nov 13 '22 05:11

personne3000