I do have one service running in the background. Whenever it starts I store in memory the starting time in milliseconds:
startingTime = new Date().getTime();
I want to display a chronometer that starts counting when the service starts and never stops until the user presses a button. I want to allow the user to leave the activity rendering the chronometer, do some stuff and then return. But the idea is that when the user returns I dont want the chronometer to go to 0:00 again. Insted I want it to show the exact time that has passed ever since the service has started.
I can calculate elapsedTime every time the user return to the chronometer activity:
elapsedTime = new Date().getTime() - startingTime;
The thing is that i dont know how to tell the chronometer to start counting from that time!
Setting it as the chronometer base does not work. Can someon explain what exactly "base" means or how to accomplish this?
thanks a lot! BYE
Some strange with SystemClock.getElapsedTime(), I did some changes for normal using with start date, like
Here child of Chronometer below, TimeView
Just copy and use, it works for me
You can use Chronometer.
You should also check this thread.
EDIT: The solution:
The base time is the time that the
Chronometer
started ticking at. You can set it usingChronometer.setBase()
. You should get the base time by usingSystemClock.getElapsedTime()
. CallsetBase()
with the start time each time theChronometer
is started. If there is potential for theActivity
to be destroyed and recreated while the timer is still active then you will need to hold the base time somewhere outside of theActivity
that owns theChronometer
.When you set the basetime with .setBase(SystemClock.elapsedRealTime()) the chronomether starts to count from 00.00 but the time it stores is the amount of milisecs from boot. When you use .stop the internal count does not stop, just the time you see on the clock. So, if you use .start again, the clock count jumps to the real count. If you want to store the time that has passed from the start, you have to get again the System elapsed time and make the difference with the .setTime
This works for me :
Use
SystemClock.elapsedRealtime()
for this purpose: