stop chronometer with a condition

Go To StackoverFlow.com

0

Now I am working on a Android application.In my application I have to use a Chronometer to show a timer.I started timer using the following code.

chrono.setBase(SystemClock.elapsedRealtime());
        chrono.start();

Now I want to call stop method when a particular time exceeds. Please help me to solve this issue friends. Thanks in advance

2012-04-04 06:29
by sarath


1

try this. it worked for me. set chrono.setOnChronometerTickListener and in that put a condition where you get the time from chronometer as a text and then do whatever you want.

chrono.setOnChronometerTickListener(new OnChronometerTickListener() {

    @Override
    public void onChronometerTick(Chronometer chronometer) {
        // TODO Auto-generated method stub
        String currentTime= chrono.getText().toString();
    if(currentTime.equals("03:00")) //put time according to you
    {
      chrono.stop();
     }
    }
});

the OnChronometerTickListener method gets called every time the chronometer has incremented.

2014-04-13 05:02
by Abhinav Raja
this line if(currenttime.equals("03:00")) the variable currenttime is typed wrong currenTime - igorlopesfaria 2016-05-23 15:11


1

Check like this :

if(currentTime.equals("3:00")){
    chrono.stop();
}

In this case you have you own time line limits say i compared with String "3:00" , you can have your conditions as per your requirements.

2012-04-04 06:40
by Venky


1

if (condition) is your friend.

2012-04-04 06:56
by PA.


0

Try like this..

save the initial time before starting your chronometer..

starttime = SystemClock.elapsedRealtime();
chrono.setBase(SystemClock.elapsedRealtime());
    chrono.start();

 timeelapsed = SystemClock.elapsedRealtime();
if(timeelapsed-starttime<5000){timeelapsed = SystemClock.elapsedRealtime();}
  chrono.stop();
2012-04-04 06:43
by 5hssba
thanks for the reply...but this not stops the chrono .. - sarath 2012-04-04 07:24
:O.. why will it not stop??there is method stop for chronometer .. right - 5hssba 2012-04-04 07:25
Ads