简体   繁体   English

如何在Android中为CountDown计时器设置完成活动?

[英]How do I set a completion activity for a CountDown timer in Android?

I am trying to create a countdown timer that counts down for 60 seconds (optionally skippable by the user). 我正在尝试创建一个倒数计时器,倒计时60秒(用户可以选择跳过)。 That part of the code works. 该部分代码有效。 How do I make it so an action is taken upon the completion of the countdown timer (the same action as the button does, ending the activity). 如何做到这一点,以便在倒数计时器完成时采取措施(与按钮相同的操作,从而结束活动)。

public void startCountdown(int total, final int increase) {
    final TimerClassExtended timer = new TimerClassExtended(total,1000);

    timer.start();


    Button skip = (Button)findViewById(R.id.skip);
    skip.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            timer.cancel();
            setResult(RESULT_OK);
            finish();
        }

    });

}

Figured it out, had to modify the following line. 弄清楚了,不得不修改以下行。 But I can't answer myself for 8 hours... 但是我八个小时都无法回答自己...

For future reference, TimerClassExtended is just a class I made that extends CountDownTimer so I could add extra methods that I needed. 为了将来参考,TimerClassExtended只是我制作的扩展CountDownTimer的类,因此我可以添加所需的其他方法。

final TimerClassExtended timer = new TimerClassExtended(total,1000) {
        public void onFinish() {
            setResult(RESULT_OK);
            finish();
        }
    };

I have no knowledge of the class you are using, but you might consider using a TimerTask and scheduling it for 60000ms. 我不了解您使用的类,但是您可能考虑使用TimerTask并将其调度为60000ms。

Timer timer = new Timer();
timer.schedule( task, 60000 );

Since I had a custom class I added the following: 由于我有一个自定义类,因此添加了以下内容:

final TimerClassExtended timer = new TimerClassExtended(total,1000) {
    public void onFinish() {
        setResult(RESULT_OK);
        finish();
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM