How can I pause one CountDown Timer when another one is running?

佐手、 提交于 2019-12-04 05:47:26

问题


I have two CountDownTimers in my program: a 4 second one, and 24 second one. I want the longer timer to be paused for every 4 seconds the shorter timer is running. Then when the short timer finishes, the long timer begins counting down. Here's the code for the two timers:

final CountDownTimer loop = new CountDownTimer(4000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                }
                @Override
                public void onFinish() {
                    number.setVisibility(View.GONE);
                    final TextView prompt = (TextView) findViewById(R.id.prompt);
                    prompt.setVisibility(View.VISIBLE);
                    prompt.setText(" Enter the number");
                    final EditText input = (EditText) findViewById(R.id.enterAnswer);
                    input.setVisibility(View.VISIBLE);
                    input.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                switch (keyCode) {
                                    case KeyEvent.KEYCODE_ENTER:
                                        Editable answer = input.getText();
                                        int finalAnswer = Integer.parseInt(String.valueOf(answer));
                                        int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText()));
                                        input.setVisibility(View.GONE);
                                        prompt.setVisibility(View.GONE);
                                        if (finalAnswer == finalLoadG1) {
                                            score++;
                                        }

                                        number.setVisibility(View.VISIBLE);
                                        int loadG1 = generateG1.nextInt(1000000)+10000;
                                        number.setText(""+loadG1);
                                        input.getText().clear();

                                        start();

                                        return true;
                                    default:
                                }
                            }
                            return false;
                        }
                    });
                }
                }.start();


            new CountDownTimer(24000, 1000) {
                @Override
                public void onTick (long millisUntilFinished) {
                }
                @Override
                public void onFinish() {
                    TextView result = (TextView) findViewById(R.id.outcome);
                    result.setText("Score: "+ score);
                    TextView prompt = (TextView) findViewById(R.id.prompt);
                    prompt.setVisibility(View.GONE);
                    final EditText input = (EditText) findViewById(R.id.enterAnswer);
                    input.setVisibility(View.GONE);
                    loop.cancel();
                    number.setVisibility(View.GONE);
                }
            }.start();

I thought about naming the longer timer, then putting name.pause() in the onTick for the shorter timer. Issue is, because the longer timer is written after the shorter one, it's name is not recognizable before it has been initialized. Java reads things in order, right? Or is that all languages?

If anyone could help me out here, I'd be grateful.

As a side question for anyone able to answer, is there any way to prevent an app from crashing when a user presses enter on an EditText (meant for an int) to submit nothing? Like, can I just have the app continue without force stopping?

Many thanks in advance.


回答1:


Create separate methods to start/pause/resume 24 second timer in your code. You need to store Your milliTillFinished of 24 seconds timer so you can pass it to resume timer method. You can pause 24 second timer on onTick() method of 4 second timer and resume it on onFinish() method.

To start timer:

   public void timerStart(long timeLengthMilli) {
    timer = new CountDownTimer(timeLengthMilli, 1000) {

        @Override
        public void onTick(long milliTillFinish) {
            milliLeft=milliTillFinish;
        }

To pause timer :

public void timerPause() {
    timer.cancel();
}

To resume timer:

private void timerResume() {
    timerStart(milliLeft);
}


来源:https://stackoverflow.com/questions/40093877/how-can-i-pause-one-countdown-timer-when-another-one-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!