暂停计时器,然后继续 [英] Pause the timer and then continue it

查看:30
本文介绍了暂停计时器,然后继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考@Yuri 从这里发布的代码.如何在一定次数后停止计时器.如果我因为某种情况想停止它,然后再次重新启动它.我该怎么做?

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

暂停:

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

在简历中:

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

这会让人产生暂停/恢复的感觉.

That makes the perception of pause/resume.

如果您的计时器根据应用程序的状态执行不同的操作,您可以考虑使用 StatePattern

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

先定义一个抽象状态:

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 );

最后,如果您需要以不同的速率执行相同的操作,您可以考虑使用 TimingFramework.它有点复杂,但让我们做很酷的动画,通过允许某些组件的绘制以不同的速率(而不是线性)进行

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 )

这篇关于暂停计时器,然后继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆