Search code examples
androidtimerstopwatchchronometer

Best way to implement Timer in Android?


I doing some math game, and I have 25 question for user to answer. So, I want to mesaure how much time that user needed for solving that?

So I need something like Timer, stopwatch with ability to pause time when user click on Back button.

I saw few implementations with Chronometer, is that good for this task?


Solution

  • If you are simply looking to figure out how long the user took you don't even need to use a special object honestly.

    Just make a long variable and store the startTime from System.getCurrentTimeInMillis();

    long startTime = System.currentTimeMillis();
    //do problems here
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    
    Log.i("TAG", "It took "+ totalTime / 1000 + " seconds to complete the problems");
    

    To do pausing you'll need a long that tracks how long it was paused and subtract that also from the totalTime.