Skip to content Skip to sidebar Skip to footer

Android Chronometer Start With Defined Value

I have an application in which I'm displaying a chronometer to the user for what he's doing. Whenever the activity goes to the background (wether by home button, or back) I save th

Solution 1:

I think you're going to have to keep track of some time marks yourself.

Chronometer myChrono;
long baseTime;
long stopTime;
long elapsedTime;

When you set the base time, you want to use:

baseTime = SystemClock.elapsedRealtime() - stopTime;
myChrono.setBase(baseTime);

When you want to see how much time has passed, use:

elapsedTime = SystemClock.elapsedRealtime() - myChrono.getBase();

Take a look at the SystemClock doc.

Solution 2:

setBase(system.currentTimeMillis() - (17 * 1000))

Should be changed to

setBase(SystemClock.elapsedRealtime() - (17 * 1000))

In general:

mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))

Hopefully this will save lots of u folks some time :)

Post a Comment for "Android Chronometer Start With Defined Value"