暂停计时器,然后继续它计时器、继续

2023-09-12 08:11:44 作者:[ 我媽生我必有用 ]

请参考code表示@Yuri贴在这里。 How停止若干倍后,一个计时器。如果我想阻止它,因为一些条件,然后再重新启动它。我怎么会去这样做呢?

 私人最终静态INT DELAY = 10000;
    私人最终处理程序处理程序=新的处理程序();
    私人最终定时器定时=新的Timer();
    私人最终TimerTask的任务=新的TimerTask(){
        私人INT计数器= 0;
        公共无效的run(){
            handler.post(新的Runnable(){
                公共无效的run(){
                    Toast.makeText(MainActivity.this,测试,Toast.LENGTH_SHORT).show();
                }
            });
            如果(++计数器== 4){
                timer.cancel();
            }

    //做一些东西在我的应用程序
   //再次重新启动计时器

        }
    };

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        timer.schedule(任务,延迟,延迟);

    }
 

下面是我尝试过,但它不断崩溃在我身上。

 最终诠释延迟= 10000;
        定时器定时;
        MyTask的任务;
        startManager扫描仪;
        处理程序处理程序;



        公共类MyTask的扩展TimerTask的{

            @覆盖
            公共无效的run(){
                handler.post(新的Runnable(){
                    公共无效的run(){
                        //做的东西在这里
                        }
        });
    }
        公共类startManager {

            公共startManager(){
                处理程序=新的处理程序();
                定时器=新的Timer();
            }

            公共无效启动(){

                timer.schedule(任务,0,时滞);
            }

            公共无效取消(){

                timer.cancel();
                timer.purge();

            }
        }

    }

 @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);

扫描仪=新startManager();
//做一些东西
 如果 (...)
Scanner.cancel()
//重新启动定时器和它的任务
扫描仪=新startManager();
        }
 

解决方案 老外发明神奇材料,能产生 空气屏障 ,纸张遇水不会湿

如果您已经取消了一个定时器,你不能重新启动它,你必须创建一个新的。

请参阅此answer,它包含一个视频源$ C ​​$ C我如何做类似的事情。

基本上有两种方法:暂停和恢复

在暂停:

 公共无效暂停(){
    this.timer.cancel();
}
 

在简历:

 公共无效简历(){
    this.timer =新的Timer();
    this.timer.schedule(aTask,0,1000);
}
 

这使得暂停/恢复的感觉。

如果您的计时器基于应用程序,你可以考虑使用 StatePattern

拳定义一个抽象状态:

 抽象类TaskState {
    公共无效的run();
    公共TaskState下一个();
}
 

和你喜欢提供尽可能多的国家。最关键的是,一个国家引导你到另一个地方。

 类的初始化状态扩展TaskState {
    公共无效的run(){
        的System.out.println(开始......);
    }
    公共TaskState下一个(){
         返回新FinalState();
    }
 }
 类FinalState扩展TaskState {
     公共无效的run(){
         的System.out.println(完成......);
     }
     公共TaskState下一个(){
         返回新的初始化状态();
    }
 }
 

然后你改变你的计时器的状态。

 定时器定时=新的Timer();
TaskState状态=新的初始化状态();

timer.schedule(新的TimerTask(){
     公共无效的run(){
          this.state.run();
          如果(shouldChangeState()){
              this.state = this.state.next();
           }
     }
 },0,1000);
 

最后,如果你需要的是执行同样的事情,但在不同的利率,你可以考虑使用 TimingFramework 。这是一个比较复杂,但我们这样做很酷的动画,通过允许一定分量的画取(而不是被直线)的地方以不同的速率

Refer to the code that @Yuri posted from here. How to stop a timer after certain number of times . If I wanted to stop it because of some condition and then restart it again. How would I go about doing it?

    private final static int DELAY = 10000;
    private final Handler handler = new Handler();
    private final Timer timer = new Timer();
    private final TimerTask task = new TimerTask() {
        private int counter = 0;
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
                }
            });
            if(++counter == 4) {
                timer.cancel();
            }

    //do some stuff in my app
   //restart the timer again

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timer.schedule(task, DELAY, DELAY);

    }

Here's what I've tried , but it keeps crashing on me.

    final int DELAY = 10000;
        Timer timer;
        MyTask task;
        startManager Scanner;
        Handler handler;



        public class MyTask extends TimerTask {

            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        //do Stuff here
                        }
        });
    }
        public class startManager {

            public startManager() {
                handler = new Handler();
                timer = new Timer();
            }

            public void start() {

                timer.schedule(task, 0, DELAY);
            }

            public void cancel() {

                timer.cancel();
                timer.purge();

            }
        }

    }

 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

Scanner = new startManager();
//do some stuff
 if (...)
Scanner.cancel()
//restart the timer and its task
Scanner=new startManager();
        }

解决方案

If you have already canceled one timer, you can't re-start it, you'll have to create a new one.

See this answer, it contains a video and the source code how I did something similar.

Basically there are two method: pause and resume

In pause:

public void pause() {
    this.timer.cancel();
}

In resume:

public void resume() {
    this.timer = new Timer();
    this.timer.schedule( aTask, 0, 1000 );
}

That makes the perception of pause/resume.

If your timers perform different actions based on the state of the application you may consider use the StatePattern

Fist define a abstract state:

abstract class TaskState  {
    public void run();
    public TaskState next();
}

And provide as many states as you like. The key is that one state leads you to another.

class InitialState extends TaskState {
    public void run() {
        System.out.println( "starting...");
    }
    public TaskState next() {
         return new FinalState();
    }
 }
 class FinalState extends TaskState  {
     public void run() {
         System.out.println("Finishing...");
     }
     public TaskState next(){
         return new InitialState();
    }
 }

And then you change the state in your timer.

Timer timer = new Timer();
TaskState state = new InitialState();

timer.schedule( new TimerTask() {
     public void run() {
          this.state.run();
          if( shouldChangeState() ) {
              this.state = this.state.next();
           }
     }
 }, 0, 1000 );

Finally, if what you need is to perform the same thing, but at different rates, you may consider using the TimingFramework. It is a bit more complex but let's you do cool animations, by allowing the painting of certain component take place at different rates ( instead of being linear )