Android實現計時與倒計時的幾種方法

來源:互聯網
上載者:User

方法一

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,綜合了前幾種方法的優點,是最簡的

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.