How can I pause the timer in android?
android
Solution
public class TimerActivity extends Activity
{
EditText e1;
MyCount counter;
Long s1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
counter= new MyCount(5000,1000);
counter.start();
}
public void asdf(View v)
{
switch(v.getId())
{
case R.id.button1: counter.cancel();
break;
case R.id.button2: counter= new MyCount(s1,1000);
counter.start();
}
}
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override public void onFinish()
{
e1.setText("DONE");
}
@Override public void onTick(long millisUntilFinished)
{
s1=millisUntilFinished;
e1.setText("left:"+millisUntilFinished/1000);
}
}
}
Problem
I have gone through the link http://dewful.com/?tag=basic-android-timer regarding the timer application in android. It is working fine. I need to add the pause button to stop the timer and a play button to start the timer again from where I stopped. Can I achieve that task? My Code: ``` long timervalue = 50000; CountDownTimer Counter1 = new CountDownTimer(timervalue, 1000) { public void onTick(final long millisUntilFinished) { time.setText(formatTime(millisUntilFinished)); pause.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Counter1.cancel(); } } ); resume.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Counter1.start(); timervalue = Long.parseLong(output); System.out.println("paused timer value resumed"+timervalue); Counter1.onTick(timervalue); } } ); } public void onFinish() { Counter1.cancel(); } }; public String formatTime(long millis) { output = "00"; seconds = millis / 1000; seconds = seconds % 60; System.out.println("seconds here"+seconds); String secondsD = String.valueOf(seconds); System.out.println("secondsD here"+secondsD); if (seconds < 10) secondsD = "0" + seconds; System.out.println("secondsD here in if"+secondsD); output = secondsD; return output; } ``` In the above code when resume button is clicked the timer is again starting from 50sec and I don't want like that. It should start from the time where I paused. Please help me regarding this...I am struggling for this since one week...... Will be really thankful for the help..........