安卓計時器

開發過程中遇到計時器的開發,記錄一下

public class TimeCountUtil
{
    private static Timer timer = null;//計時器
    private static TimerTask timerTask = null;
    private static long i = 1000;


    private TimeCountUtil()
    {
    }

    private static TimeCountUtil instance = new TimeCountUtil();

    public static TimeCountUtil getInstance()
    {
        return instance;
    }

    //開始計時
    public void startTimeCount(final Handler mHandler)
    {
        if (timer == null)
        {
            timer = new Timer();

        }

        timerTask = new TimerTask()
        {
            @Override
            public void run()
            {
                i += 1000;
                Message message = Message.obtain();
                message.arg1 = (int) i;
                mHandler.sendMessage(message);//發送消息
            }
        };
        timer.schedule(timerTask, 1000);//1000ms執行一次
        Log.e(AnBangApp.TAG,"startTimeCount :"+i);
    }

    //結束計時
    public void stopTimeCount()
    {
        if (null != timerTask)
        {
            timerTask.cancel();
            timerTask = null;
        }
        if (null != timer)
        {
            timer.cancel();
            timer = null;
        }
    }

    //返回當前記錄的時間
//    public  String getCurrentCountTime()
//    {
//        Log.e(AnBangApp.TAG,"getCurrentCountTime :"+confDurTime);
//        return confDurTime;
//    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章