取消并重新启动CountDownTimer问题重新启动、问题、CountDownTimer

2023-09-06 05:29:52 作者:予你心安″

你好我有CountDownTimer功能的问题。

首先,我可以得到它停止与counter.cancel倒计时();然后,我存储milliUntilFinished值countercur。

然后我重新使用该存储countercur值计时器。这一切工作正常。但是当我尝试取消第二次,它永远不会再停止计时器。只是工作一次,我失去的东西吗?这里是我的code感谢:

  //主要code:mycount的计数器=新mycount的(59000,1000);            counter.start(); //在59秒内开机定时器///button1.setOnClickListener(新View.OnClickListener(){    公共无效的onClick(视图v){counter.cancel(); //  - >取消计时器在这里工作时,时钟停止OK//// code的其余部分剪断button2.setOnClickListener(新View.OnClickListener(){       公共无效的onClick(视图v){      mycount的计数器=新mycount的(countcur,1000); // countcur是当前计数器值最后取消的时候           counter.start(); //这将重新启动计时器确定。当重试时1按钮再次取消,它永远不会取消。// code的其余部分剪断//定时器设置    公共类mycount的扩展CountDownTimer {        公共mycount的(长millisInFuture,长countDownInterval){        超(mil​​lisInFuture,countDownInterval);        }        公共无效onFinish(){意向意图=新的Intent();            intent.setClass(Main.this,Final.class);            startActivity(意向);            }            其他{            mycount的计数器=新mycount的(59000,1000);            counter.start();            时钟= counter.toString();            }        }        公共无效onTick(长millisUntilFinished){            // INT分= 5;            / *如果(+ millisUntilFinished / 1000℃= 240){                分= 4;                mintosec =240;                // millisUntilFinished = 59000;            }            否则如果(+ millisUntilFinished / 1000℃; = 180){                分= 3;                mintosec =180; }            否则如果(+ millisUntilFinished / 1000℃; = 120){                分= 2;                mintosec =120; }            否则如果(+ millisUntilFinished / 1000℃= 60)                分= 1;                mintosec =60; * /            // TextView的totmin =(的TextView)findViewById(R.id.clockmin);            //totmin.setText();            TextView的totsec =(的TextView)findViewById(R.id.clocksec);            totsec.setText(+时间+:+ millisUntilFinished / 1000);            countcur = millisUntilFinished;暂停和重启//存储当前的计数值        }    } //结束定时器 

解决方案

在下面,要设置一个局部变量,而不是类变量:

  button2.setOnClickListener(新View.OnClickListener(){       公共无效的onClick(视图v){      mycount的计数器=新mycount的(countcur,1000); // countcur是当前计数器值最后取消的时候 
Countdown Timer

将其更改为引用现有的类变量,而不是:

  button2.setOnClickListener(新View.OnClickListener(){       公共无效的onClick(视图v){     计数器=新mycount的(countcur,1000); // countcur是当前计数器值最后取消的时候 

Hi I am having a problem with CountDownTimer function

first I can get it to stop counting down with the counter.cancel(); then I store the milliUntilFinished value in countercur.

then I restart the timer using that stored countercur value. This all works fine. but when I try to cancel a 2nd time, it never stops the timer anymore. just works once, am I missing something? here is my code Thanks:

// Main code :
MyCount counter = new MyCount(59000, 1000);
            counter.start(); // start timer at 59 seconds

///

button1.setOnClickListener(new View.OnClickListener() { 


    public void onClick(View v) {
counter.cancel(); // -- > cancelling the timer works here, the clock stops ok
//
// rest of code snipped

button2.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) {
      MyCount counter= new MyCount(countcur,1000); // countcur is the current counter value when last cancelled

           counter.start(); // This restarts timer ok. when when retrying button 1 to cancel again, it never cancels.

// rest of code snipped

// Timer Setup
    public class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {

        super(millisInFuture, countDownInterval);
        }    
        public void onFinish() {
Intent intent = new Intent(); 
            intent.setClass(Main.this, Final.class);
            startActivity(intent);
            }
            else {
            MyCount counter = new MyCount(59000, 1000);
            counter.start();
            clock = counter.toString();
            }

        }    
        public void onTick(long millisUntilFinished) {
            //int min = 5;
            /*if (+millisUntilFinished / 1000 <= 240) {
                min = 4;
                mintosec = "240";

                //millisUntilFinished = 59000;
            }
            else if (+millisUntilFinished / 1000 <= 180) {
                min = 3;
                mintosec = "180"; }
            else if (+millisUntilFinished / 1000 <= 120) {
                min = 2;
                mintosec = "120"; }
            else if (+millisUntilFinished / 1000 <= 60)
                min =1;
                mintosec = "60"; */

            //TextView totmin = (TextView) findViewById(R.id.clockmin);
            //totmin.setText("");
            TextView totsec = (TextView) findViewById(R.id.clocksec);
            totsec.setText(+duration +" : "+ millisUntilFinished / 1000);
            countcur = millisUntilFinished; // store current count value for pausing and restarting
        }
    }  // End Timer

解决方案

In the following, you are setting a local variable instead of the class variable:

button2.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) {
      MyCount counter= new MyCount(countcur,1000); // countcur is the current counter value when last cancelled

Change it to reference the existing class variable instead:

button2.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) {
     counter= new MyCount(countcur,1000); // countcur is the current counter value when last cancelled

 
精彩推荐
图片推荐