RaagaSudha
RaagaSudha

Reputation: 397

How can I pause the timer in android?

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..........

Upvotes: 2

Views: 5281

Answers (2)

5hssba
5hssba

Reputation: 8079

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);
        }
    }
}

Upvotes: 10

Khan
Khan

Reputation: 7605

public void time(long m){
    timer=new CountDownTimer(m, 1000) {

            public void onTick(long millisUntilFinished) {

                tv_timer.setText(formatTime(millisUntilFinished));

            }

            public void onFinish() {
                tv_timer.setText("done!");
            }

         }.start();

 }

i have used this for timer and i have put button for pause and use this

    case R.id.bT_PAUSE:
    String s_time = null;

    try{
    if(Bt_pause.getText().equals("PAUSE")){

    s_time=tv_timer.getText().toString();
    timer.cancel();
    String[] Pause_time=s_time.split(":");
    m=Long.parseLong(Pause_time[0].trim());
    n=Long.parseLong(Pause_time[1].trim());
    m=(m*60)+n;
    m=m*1000;
    Bt_pause.setText("RESUME");

    }else if(Bt_pause.getText().equals("RESUME")){
        //min_longmillis=Long.parseLong(sss);
        //min_longmillis=min_longmillis*1000*60;
        //min_longmillis=m;
        //timer.start();
        Toast.makeText(this,String.valueOf(m),Toast.LENGTH_SHORT).show();
        time(m);
        Bt_pause.setText("PAUSE");
    }
    }catch(Exception e){
        Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();
    }
    break;

and this for format time

public String formatTime(long millis) {  
    String output = "00:00";  
    long seconds = millis / 1000;  
    long minutes = seconds / 60;  

    seconds = seconds % 60;  
    minutes = minutes % 60;  

    String sec = String.valueOf(seconds);  
    String min = String.valueOf(minutes);  

    if (seconds < 10)  
        sec = "0" + seconds;  
    if (minutes < 10)  
        min= "0" + minutes;  

    output = min + " : " + sec;  
    return output;
}//formatTime

Upvotes: 0

Related Questions