方法一
Timer與TimerTask(Java實現)
public class timerTask extends Activity{</p><p>private int recLen = 11;<br />private TextView txtView;<br />Timer timer = new Timer();</p><p>public void onCreate(Bundle savedInstanceState){<br />super.onCreate(savedInstanceState);</p><p>setContentView(R.layout.timertask);<br />txtView = (TextView)findViewById(R.id.txttime);</p><p>timer.schedule(task, 1000, 1000);// timeTask<br />}</p><p>TimerTask task = new TimerTask() {<br />@Override<br />public void run() {</p><p>runOnUiThread(new Runnable() {// UI thread<br />@Override<br />public void run() {<br />recLen--;<br />txtView.setText(""+recLen);<br />if(recLen < 0){<br />timer.cancel();<br />txtView.setVisibility(View.GONE);<br />}<br />}<br />});<br />}<br />};<br />}
方法二
TimerTask與Handler(不用Timer的改進型)
public class timerTask extends Activity{<br />private int recLen = 11;<br />private TextView txtView;<br />Timer timer = new Timer();</p><p>public void onCreate(Bundle savedInstanceState){<br />super.onCreate(savedInstanceState);</p><p>setContentView(R.layout.timertask);<br />txtView = (TextView)findViewById(R.id.txttime);</p><p>timer.schedule(task, 1000, 1000);// timeTask<br />}</p><p>final Handler handler = new Handler(){<br />@Override<br />public void handleMessage(Message msg){<br />switch (msg.what) {<br />case 1:<br />txtView.setText(""+recLen);<br />if(recLen < 0){<br />timer.cancel();<br />txtView.setVisibility(View.GONE);<br />}<br />}<br />}<br />};</p><p>TimerTask task = new TimerTask() {<br />@Override<br />public void run() {<br />recLen--;<br />Message message = new Message();<br />message.what = 1;<br />handler.sendMessage(message);<br />}<br />};<br />}
方法三
Handler與Message(不用TimerTask)
public class timerTask extends Activity{<br />private int recLen = 11;<br />private TextView txtView;</p><p>public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState); </p><p>setContentView(R.layout.timertask);<br />txtView = (TextView)findViewById(R.id.txttime);</p><p>Message message = handler.obtainMessage(1);// Message<br />handler.sendMessageDelayed(message, 1000);<br />} </p><p>final Handler handler = new Handler(){</p><p>public void handleMessage(Message msg){// handle message<br />switch (msg.what) {<br />case 1:<br />recLen--;<br />txtView.setText("" + recLen);</p><p>if(recLen > 0){<br />Message message = handler.obtainMessage(1);<br />handler.sendMessageDelayed(message, 1000);// send message<br />}else{<br />txtView.setVisibility(View.GONE);<br />}<br />}</p><p>super.handleMessage(msg);<br />}<br />};<br />}
方法四
Handler與Thread(不佔用UI線程)
public class timerTask extends Activity{<br />private int recLen = 0;<br />private TextView txtView;</p><p>public void onCreate(Bundle savedInstanceState){<br />super.onCreate(savedInstanceState);</p><p>setContentView(R.layout.timertask);<br />txtView = (TextView)findViewById(R.id.txttime);</p><p>new Thread(new MyThread()).start();// start thread<br />}</p><p>final Handler handler = new Handler(){// handle<br />public void handleMessage(Message msg){<br />switch (msg.what) {<br />case 1:<br />recLen++;<br />txtView.setText("" + recLen);<br />}<br />super.handleMessage(msg);<br />}<br />};</p><p>public class MyThread implements Runnable{// thread<br />@Override<br />public void run(){<br />while(true){<br />try{<br />Thread.sleep(1000);// sleep 1000ms<br />Message message = new Message();<br />message.what = 1;<br />handler.sendMessage(message);<br />}catch (Exception e) {<br />}<br />}<br />}<br />}<br />}
方法五
Handler與Runnable(最簡單型)
public class timerTask extends Activity{<br />private int recLen = 0;<br />private TextView txtView;</p><p>public void onCreate(Bundle savedInstanceState){<br />super.onCreate(savedInstanceState);</p><p>setContentView(R.layout.timertask);<br />txtView = (TextView)findViewById(R.id.txttime);</p><p>handler.postDelayed(runnable, 1000);<br />}</p><p>Handler handler = new Handler();<br />Runnable runnable = new Runnable() {<br />@Override<br />public void run() {<br />recLen++;<br />txtView.setText("" + recLen);<br />handler.postDelayed(this, 1000);<br />}<br />};<br />}
計時與倒計時
方法1,方法2和方法3,都是倒計時
方法4,方法5,都是計時
計時和倒計時,都可使用上述方法實現(代碼稍加改動)
UI線程比較
方法1,方法2和方法3,都是在UI線程實現的計時;
方法4和方法5,是另開Runnable線程實現計時
實現方式比較
方法1,採用的是Java實現,即Timer和TimerTask方式;
其它四種方法,都採用了Handler訊息處理
推薦使用
如果對UI線程互動要求不很高,可以選擇方法2和方法3
如果考慮到UI線程阻塞,嚴重影響到使用者體驗,推薦使用方法4,另起線程單獨用於計時和其它的邏輯處理
方法5,綜合了前幾種方法的優點,是最簡的