我无法根据条件

时间:2017-11-15 19:13:32

标签: android countdowntimer pause

我正在创建一个应用程序,当特定条件存在时,它会暂停倒数计时器。我的倒数计时器循环一个字符串数组来填充文本字段。每次计数器降至零时,它都会使用下一组文本重置并填充文本字段。我甚至有一个工作暂停按钮。但是,我无法根据其中一个文本字段中的特定文本以编程方式暂停倒数计时器。

继承我的计时器代码:

class MyTimer extends CountDownTimer {

    //constructor for timer class
    public MyTimer(long millisInFurture, long countDownInterval) {
        super(millisInFurture, countDownInterval);
    }

    // this method called when timer is finished
    @Override
    public void onFinish() {
        // reset all variables
        clockText.setText(clockTime);
        isRunning = false;
        remainMilli = 0;

        advanceLevel();
        goTimer();
    }

    // this method is called for every iteration of time interval
    @Override
    public void onTick(long millisUntilFinished) {
        remainMilli = millisUntilFinished;

        //calculate minutes and seconds from milliseconds
        String minute = "" + (millisUntilFinished/1000)/60;
        String second = "" + (millisUntilFinished/1000)%60;

        // apply style to minute and second
        if((millisUntilFinished/1000)/60 < 10) {
            minute = "0" + (millisUntilFinished/1000)/60;
        }
        if ((millisUntilFinished/1000)%60 < 10) {
            second = "0" + (millisUntilFinished/1000)%60;
        }

        //update textview with remaining time
        clockText.setText(minute + ":" + second);
    }

}

这是我的计时器的开始和暂停代码:

public boolean goTimer() {
    breakCheck = blinds.getText().toString();
    Log.i(breakCheck, "level");

    if (isRunning) {
        // cancel (Pause) timer when it is running
        mTimer.cancel();
        mTimer = null;
        isRunning = false;
    } else {
        if (remainMilli == 0) {
            // start timer from initial time
            mTimer = new MyTimer(blindTime, 1000);

        } else {
            //resume timer from where it is paused
            mTimer = new MyTimer(remainMilli, 1000);
        }
        mTimer.start();
        isRunning = true;
        }

    return true;

}

...这里是在倒数计时器的每个循环之后更改文本字段的代码:

public boolean advanceLevel() {
    levelNum = levelNum + 1;
    round = round + 1;
    anteLevel = anteLevel + 1;

    if (round < roundMax) {
        level.setText(String.valueOf(levelNum));
        blinds.setText(blinds_list[round]);
        ante.setText(ante_list[anteLevel]);
    } else if (round == roundMax) {
        level.setText("Max Level");

    } else if (round > roundMax) {
        try {
            mTimer.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }
    return true;
}

因此,每次倒计时结束时,文本字段都会填充String-array中的新文本。我想要的是当百叶窗文本字段显示&#34; BREAK&#34;时,我希望倒数计时器暂停。

所以,我输了一个:

String blindCheck = blinds.getText().toString();

if(blindCheck ==&#34; BREAK&#34;){mTimer.cancel(); }

...但无论我做什么,倒数计时器都不会暂停,它会继续前进。

计时器的“开始/暂停”按钮位于底部导航栏上,效果很好,所以当我的blindCheck ==&#34; BREAK&#34;时,我尝试让它执行启动/暂停时钟的方法,但那也不起作用。

我不知道该怎么做。任何帮助,将不胜感激。 谢谢, Hendo

1 个答案:

答案 0 :(得分:0)

不要将CountDownTimer用于此目的。而不是这样,你应该像这样使用Handler

final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {

            // done your functionality here


            // if you want to continue, or pause is not active
            handler.postDelayed(this, 1*1000);

            // else you will in pause state

        }
    };


    // to start the hanlder
    handler.postDelayed(runnable, 1*1000);

    // to stop or pause the handler
    handler.removeCallbacks(runnable);