美文网首页
Android Timer

Android Timer

作者: aaa000 | 来源:发表于2019-07-28 21:55 被阅读0次
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class DSTimer {
    
        private OnTimerFiredListener listener;
        private Timer timer;
        private TimerTask timerTask;
        private boolean isPause = false;
    
        public void openTimer(int t, OnTimerFiredListener a)
        {
            listener = a;
            timer = new Timer();
            timerTask = new TimerTask(){
                @Override
                public void run()
                {
                    try {if (isPause==false)  listener.timerFired();}
                    catch (Exception e){e.printStackTrace();}
                }
            };
            timer.schedule(timerTask,0,t*1000);
        }
    
        public void pause()
        {
            isPause= true;
        }
        public void cannel()
        {
            timer.cancel();
            if (timer!=null) timer=null;
            if (timerTask!=null)timerTask=null;
        }
        public void start()
        {
            isPause = false;
        }
    
    }
    interface OnTimerFiredListener{
        void timerFired();
    }
    

    相关文章

      网友评论

          本文标题:Android Timer

          本文链接:https://www.haomeiwen.com/subject/iokmrctx.html