開發過程中遇到計時器的開發,記錄一下
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;
// }
}