user4301080
user4301080

Reputation:

pause and resume timer without reseting the timer

is there any simple method that when the timer is paused then started again it will resume from the time that it was paused at?

i looked at other tutorials and they only restart the timer once the pause button is pressed twice.

Im new to android development so sorry if this is a silly question.

public class Timer extends Fragment {

TextView CurrentDate;
TextView CurrentTime;
Thread ThreadTime;
TextView textTimeStarted;
Button buttonReset;
Button buttonPauseStart;
TextView textElapsedTime;
private Button startButton;
private Button pauseButton;
private TextView timerValue;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
long startTime = 0L;

//runs without a timer by reposting this handler at the end of the runnable
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
    @Override
    public void run() {
        long millis = System.currentTimeMillis() - startTime;
        int millisecs = (int) (millis % 1000);
        int seconds = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;

        textElapsedTime.setText(String.format("%d:%02d:%02d", minutes, seconds, millisecs));

        timerHandler.postDelayed(this, 0);
    }
};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View T = inflater.inflate(R.layout.time_layout, container, false);

    Runnable myRunnableThread = new CountDownRunner();
    ThreadTime = new Thread(myRunnableThread);
    ThreadTime.start();
    ConnecttoXML(T);
    buttonReset = (Button) T.findViewById(R.id.buttonStop);
    buttonReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            timerHandler.removeCallbacks(timerRunnable);
            textElapsedTime.setText("");
            textTimeStarted.setText("");
        }
    });

    buttonPauseStart = (Button) T.findViewById(R.id.ButtonStart);
    buttonPauseStart.setText("Start");


    buttonPauseStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            buttonPauseStart = (Button) v;
            if (buttonPauseStart.getText().equals("stop")) {
                timerHandler.removeCallbacks(timerRunnable);
                buttonPauseStart.setText("start");

            } else  {

                startTime = System.currentTimeMillis();
                timerHandler.postDelayed(timerRunnable, 0);
                Calendar cs = Calendar.getInstance();
                System.out.println("Current time => " + cs.getTime());

                SimpleDateFormat df = new SimpleDateFormat("HH:mm");
                String formattedDate = df.format(cs.getTime());

                textTimeStarted.setText(formattedDate);
                buttonPauseStart.setText("stop");
            }
        }
    });

    return T;
}

private void ConnecttoXML(View view) {

    CurrentDate = (TextView) view.findViewById(R.id.textCurrentDate);
    textTimeStarted = (TextView) view.findViewById(R.id.textTimeStarted);
    textElapsedTime = (TextView) view.findViewById(R.id.textElapsedTime);

    String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());
    CurrentDate.setText(currentDate);
}

public void doWork() {
    getActivity().runOnUiThread(new Runnable() {
        public void run() {
            try {
                CurrentTime = (TextView) getActivity().findViewById(R.id.textCurrentTime);
                Date dt = new Date();
                int hours = dt.getHours();
                int minutes = dt.getMinutes();
                String curTime = hours + ":" + minutes;
                CurrentTime.setText(curTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public class CountDownRunner implements Runnable {
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                doWork();
                Thread.sleep(1000); // Pause of 1 Second
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (Exception e) {
            }
        }
    }
}
}

Upvotes: 0

Views: 880

Answers (1)

Gary Bak
Gary Bak

Reputation: 4798

No, for the Handler you can only set the time and then cancel it, but I don't see how that would help you out anyway since you are running the postDelayed at 0, essentially as fast as possible to update the timer in your UI.

If you want to pause your timer, you'll have to save the elapsed time instead of the startTime and have start/stop and reset button.

Here is the easiest way I found to add the pause to your timer:

buttonPauseStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            buttonPauseStart = (Button) v;
            if (buttonPauseStart.getText().equals("stop")) {
                elapsedTime = System.currentTimeMillis() - startTime;
                timerHandler.removeCallbacks(timerRunnable);
                buttonPauseStart.setText("start");

            } else  {

                startTime = System.currentTimeMillis() - elapsedTime;
                timerHandler.postDelayed(timerRunnable, 0);
                Calendar cs = Calendar.getInstance();
                System.out.println("Current time => " + cs.getTime());

                SimpleDateFormat df = new SimpleDateFormat("HH:mm");
                String formattedDate = df.format(cs.getTime());

                textTimeStarted.setText(formattedDate);
                buttonPauseStart.setText("stop");
            }
        }
    });

Upvotes: 1

Related Questions