Android--三種實現定時器的方法

來源:互聯網
上載者:User

標籤:

方法一:Handler+Thread

handler   處理者,管理者。主要接受子線程發送的資料, 並用此資料配合主線程更新UI.

 

package com.xunfang.handerDemo;    import android.app.Activity;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message;  import android.widget.TextView;    /**  * handler定時器  *   * @author Smalt  *   */  public class HanderDemoActivity extends Activity {      TextView tvShow;      private int i = 0;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          tvShow = (TextView) findViewById(R.id.tv_show);          new Thread(new ThreadShow()).start();      }        // handler類接收資料      Handler handler = new Handler() {          public void handleMessage(Message msg) {              if (msg.what == 1) {                  tvShow.setText(Integer.toString(i++));                  System.out.println("receive....");              }          };      };        // 線程類      class ThreadShow implements Runnable {            @Override          public void run() {              // TODO Auto-generated method stub              while (true) {                  try {                      Thread.sleep(1000);                      Message msg = new Message();                      msg.what = 1;                      handler.sendMessage(msg);                      System.out.println("send...");                  } catch (Exception e) {                      // TODO Auto-generated catch block                      e.printStackTrace();                      System.out.println("thread error...");                  }              }          }      }  } 

方法二:Handler類內建的postDelyed

 

package com.xunfang.handerDemo;    import android.app.Activity;  import android.os.Bundle;  import android.os.Handler;  import android.widget.TextView;    /**  * handler定時器使用postDelyed實現  *   * @author Smalt  *   */  public class HanderDemoActivity extends Activity {      TextView tvShow;      private int i = 0;      private int TIME = 1000;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          tvShow = (TextView) findViewById(R.id.tv_show);          handler.postDelayed(runnable, TIME); //每隔1s執行      }        Handler handler = new Handler();      Runnable runnable = new Runnable() {            @Override          public void run() {              // handler內建方法實現定時器              try {                  handler.postDelayed(this, TIME);                  tvShow.setText(Integer.toString(i++));                  System.out.println("do...");              } catch (Exception e) {                  // TODO Auto-generated catch block                  e.printStackTrace();                  System.out.println("exception...");              }          }      };    }  

方法三:

Handler+Timer+TimerTask

package com.xunfang.handerDemo;    import java.util.Timer;  import java.util.TimerTask;    import android.app.Activity;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message;  import android.widget.TextView;    /**  * 定時器實現:Handler+Timer+TimerTask  *   * @author Smalt  *   */  public class HanderDemoActivity extends Activity {      TextView tvShow;      private int i = 0;      private int TIME = 1000;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          tvShow = (TextView) findViewById(R.id.tv_show);          timer.schedule(task, 1000, 1000); // 1s後執行task,經過1s再次執行      }        Handler handler = new Handler() {          public void handleMessage(Message msg) {              if (msg.what == 1) {                  tvShow.setText(Integer.toString(i++));              }              super.handleMessage(msg);          };      };      Timer timer = new Timer();      TimerTask task = new TimerTask() {            @Override          public void run() {              // 需要做的事:發送訊息              Message message = new Message();              message.what = 1;              handler.sendMessage(message);          }      };  }  

 

 

 

Android--三種實現定時器的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.