android 定時器

定時器的三種方法。

(1)Handler

//開始定時
	public void startTimer() {
		//移除MSG_TIMING
		mHandler.removeMessages(MSG_TIMING);
		Message msg = Message.obtain();
		msg.what = MSG_TIMING;
		msg.arg1 = TYPE_START;
		//開始計時
		mHandler.sendMessage(msg);
	}

	//停止計時
	public void stopTimer() {
		//移除MSG_TIMING
		mHandler.removeMessages(MSG_TIMING);
	}

	public static final int MSG_TIMING = 1;
	public static final int TYPE_START = 1;
	public static final int TYPE_STOP = 2;

	private Handler mHandler = new Handler() {

		private int count = 0;

		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			int type = msg.arg1;
			if (what == MSG_TIMING) {
				if (type == TYPE_START) {
					count = 0;
				}
				// UI
				// UI
				//延遲1000ms
				mHandler.sendEmptyMessageDelayed(MSG_TIMING, 1000);
				count++;
			}
		}

	};

(2)Handler和Thread

//開始定時
	public void startTimer() {
		mThread = new MyThread();
		mThread.start();
	}


	//停止計時
	public void stopTimer() {
		if(mThread != null) {
			mThread.setStop(true);
			mThread = null;
		}
	}


	public static final int MSG_TIMING = 1;
	public static final int TYPE_START = 1;
	public static final int TYPE_STOP = 2;
	
	class MyThread extends Thread {
		
		private boolean mStop = false;
		private int count = 0;
		
		public void setStop(boolean stop) {
			stop = true;
		}
		
		@Override
		public void run() {
			
			while(!mStop) {
				Message msg = Message.obtain();
				msg.arg1 = count;
				msg.what = MSG_TIMING;
				mHandler.sendMessage(msg);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				count ++;
			}
		}
	}
	
	private MyThread mThread;


	private Handler mHandler = new Handler() {


		private int count = 0;


		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			int count = msg.arg1;
			//UI
		}


	};

(3)Timer、TimerTak和Handler

public static final int MSG_TIMER = 0;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			if (what == MSG_TIMER) {
				mTimerResult = msg.arg1;
				mTimingText.setText(Integer.toString(mTimerResult));
			}
		};
	};

	private Timer mTimer;
	TimerTask mTask;

	private int mTimerResult = 0;
	private boolean mIsTiming = false;
	private boolean mStarted = false;

	public void startTimer() {
		mIsTiming = true;
		restartTimer();
	}

	public void restartTimer() {
		mHandler.removeMessages(MSG_TIMER);
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
		if (mTask != null) {
			mTask.cancel();
			mTask = null;
		}
		mTimer = new Timer();
		mTask = new TimerTask() {

			private int count = 0;

			@Override
			public void run() {
				Message msg = Message.obtain();
				msg.what = MSG_TIMER;
				msg.arg1 = count;
				mHandler.sendMessage(msg);
				count++;
			}
		};
		mTimer.schedule(mTask, 0, 1000);
	}

	public void stopTimer() {
		mIsTiming = false;
		mHandler.removeMessages(MSG_TIMER);
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
		if (mTask != null) {
			mTask.cancel();
			mTask = null;
		}
	}


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