private Handler mHandler = new Handler() { public void handleMessage(Message msg) { int arg1 = msg.arg1; if (arg1 == 0) {//時間到 } textView.setText(String.valueOf(arg1)); } }; /** * 開始自動減時 */ private void startTime() { if (timer == null) { timer = new Timer(); } timerTask = new TimerTask() { @Override public void run() { Message message = Message.obtain(); message.arg1 = time; mHandler.sendMessage(message);//發送消息 time--;//自動減1 } }; timer.schedule(timerTask, 0, 1000);//1000ms執行一次 } private void stopTime() { if (timer != null) { timer.cancel(); timer = null; } if (timerTask != null) { timerTask.cancel(); timerTask = null; } time = 0; }