How to implement pause and resume method in countdowntimer

Hi I am using the countdown concept. Here, when I press the start button, the countdown will start. and also to stop it working, but I need to implement a pause and resume method during the countdown timer. the countdown will pause when I press the pause button. the countdown will start from the pause value when I press the start button. eg: my countdown is from 0 to 30, suppose if I press the pause button, the value is paused: 25 or 15 whatever. the countdown will start at 25 when I press the start button. I hope you all understand my problem that I have tried but I am not getting any solution, can you please help me.

  public class MainActivity extends ActionBarActivity implements OnClickListener {

     private CountDownTimer countDownTimer;
     private boolean timerHasStarted = false;
     private Button startB;
     public TextView text;
     private final long startTime = 30 * 1000;
     private final long interval = 1 * 1000;


     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    startB = (Button) this.findViewById(R.id.button);
    startB.setOnClickListener((OnClickListener) this);
    text = (TextView) this.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval);
    text.setText(text.getText() + String.valueOf(startTime / 1000));
   }

    public void onClick(View v) {
     if (!timerHasStarted) {
      countDownTimer.start();
      timerHasStarted = true;
      startB.setText("STOP");
      } else {
        countDownTimer.cancel();
        timerHasStarted = false;
        startB.setText("RESTART");
    }
    } 
 public class MyCountDownTimer extends CountDownTimer {
   public MyCountDownTimer(long startTime, long interval) {
                   super(startTime, interval);
  }

   @Override
    public void onFinish() {
     //text.setText("Time up!");
      countDownTimer.start();
   }

     @Override
      public void onTick(long millisUntilFinished) {
      text.setText("" + millisUntilFinished / 1000);
         }
       }
    }


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/rl" >

    <TextView
    android:id="@+id/timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:paddingRight="10dip"
    android:textSize="50dp" />

    <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Start" />

</RelativeLayout>

      

+3


source to share


1 answer


The only way I can see is to cancel the timer when pause is called and create a new one when you want to resume it, something like this:



public class MainActivity extends ActionBarActivity implements OnClickListener {

 private MyCountDownTimer countDownTimer;
 private boolean timerHasStarted = false;
 private Button startB;
 public TextView text;
 private final long startTime = 30 * 1000;
 private final long interval = 1 * 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startB = (Button) this.findViewById(R.id.button);
    startB.setOnClickListener(this);

    text = (TextView) this.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval,text );
    // Set starting value
    text.setText(text.getText() + String.valueOf(startTime / 1000));
}

 public void onClick(View v) {
     if (!timerHasStarted) {
      // Start or resume counter
      countDownTimer = countDownTimer.resume();
      timerHasStarted = true;
      startB.setText("PAUSE");
      } else {
       // Pause counter
        countDownTimer.pause();
        timerHasStarted = false;
        startB.setText("RESTART");
      }
}

public class MyCountDownTimer extends CountDownTimer
{

    private long mRemainingTime = 0;
    private long mInterval = 0;
    private TextView mtvxToUpdate = null

    //  Use a textview reference to update text on each tick()
    public MyCountDownTimer(long startTime, long interval,TextView txv ) {
        super(startTime, interval);
        this.mInterval = interval;
        this.mRemainingTime = startTime;
        this.mtvxToUpdate = txv;
    }

    @Override
    public void onFinish() {
        mtvxToUpdate.setText("Time up!");
    }

    @Override
    public void onTick(long millisUntilFinished) {
            mRemainingTime = millisUntilFinished;           
            mtvxToUpdate.setText("" + millisUntilFinished / 1000);
    }


    public void pause(){
        // Stop current timer 
        this.cancel();
    }

    public MyCountDownTimer resume(){
        // Create a counter with last saved data (just before pause)
        MyCountDownTimer newTimer = new MyCountDownTimer(mRemainingTime,mInterval,mtvxToUpdate);
        // Start this new timer that start where old one stop
        newTimer.start();
        // Return this new timer
        return newTimer;
    }
}
}

      

+1


source







All Articles