Question

how to pause the countDown timer and when i click start it start from the last point that stopped on it and want the pauseButton pause this countdown timer in the same click on pause the countup timer also . so what is the code for this ?

public class one extends Activity {
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
TextView mButtonLabel;
private long mStartTime = 0L;
private TextView mTimeLabel, mTimerLabel;
private Handler mHandler = new Handler();
static final int UPDATE_INTERVAL = 1000;
String timerStop1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one);

    mTimeLabel = (TextView) findViewById(R.id.countDownTv);
    mButtonLabel = (TextView) findViewById(R.id.countDown);
    // mTimerLabel = (TextView) findViewById(R.id.textTimer);
    mButtonLabel = (TextView) findViewById(R.id.countDown);
    timerValue = (TextView) findViewById(R.id.timerValue);
    startButton = (Button) findViewById(R.id.startButton);

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);
            CountDownTimer timer1 = new CountDownTimer(1800000, 1000) {

                @Override
                public void onFinish() {

                    mTimeLabel.setText("Done");
                }

                @Override
                public void onTick(long millisUntilFinished) {

                    int seconds = (int) (millisUntilFinished / 1000);
                    int minutes = seconds / 60;
                    seconds = seconds % 60;

                    mTimeLabel.setText("" + minutes + ":"
                            + String.format("%02d", seconds));

                }
            }.start();

        }
    });

    pauseButton = (Button) findViewById(R.id.pauseButton);

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);

            // here i want to put the code that will make this click pause the countdown timer also 
        }

    });

}

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":" + String.format("%02d", secs)
                + ":" + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }

};



private Runnable mUpdateTimeTask = new Runnable() {

    public void run() {

        final long start = mStartTime;
        long millis = SystemClock.uptimeMillis() - start;

        int seconds = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;

        mTimerLabel.setText("" + minutes + ":"
                + String.format("%02d", seconds));

        timerStop1 = minutes + ":" + String.format("%02d", seconds);

        mHandler.postDelayed(this, 200);

    }
};

}

Was it helpful?

Solution

The most brain dead solution I can think of is saving the countdown timer's current value in the onTick() method and then setting up the timer again based on this value.

EDIT:

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

public void resume() {
    this.setupTimer(timeLeft, interval);
}

Additionally, you can write some code that you can call when your button gets clicked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top