Android Timer

2019-07-28  本文已影响0人  aaa000

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();
}
上一篇下一篇

猜你喜欢

热点阅读