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
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.
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.
if (condition)
is your friend.
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();