Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現計時與倒計時的幾種方法 Timer與TimerTask

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

編輯:關於Android編程

方法一 Timer與TimerTask(Java實現) public class timerTask extends Activity{  private int recLen = 11;  private TextView txtView;  Timer timer = new Timer();  public void onCreate(Bundle savedInstanceState){   super.onCreate(savedInstanceState);      setContentView(R.layout.timertask);   txtView = (TextView)findViewById(R.id.txttime);      timer.schedule(task, 1000, 1000);  // timeTask  }   TimerTask task = new TimerTask() {   @Override   public void run() {    runOnUiThread(new Runnable() {  // UI thread     @Override     public void run() {      recLen--;      txtView.setText(""+recLen);      if(recLen < 0){       timer.cancel();       txtView.setVisibility(View.GONE);      }     }    });   }  }; }   方法二 TimerTask與Handler(不用Timer的改進型) public class timerTask extends Activity{  private int recLen = 11;  private TextView txtView;  Timer timer = new Timer();  public void onCreate(Bundle savedInstanceState){   super.onCreate(savedInstanceState);   setContentView(R.layout.timertask);   txtView = (TextView)findViewById(R.id.txttime);   timer.schedule(task, 1000, 1000);  // timeTask  }   final Handler handler = new Handler(){   @Override   public void handleMessage(Message msg){    switch (msg.what) {    case 1:     txtView.setText(""+recLen);     if(recLen < 0){      timer.cancel();      txtView.setVisibility(View.GONE);     }    }   }  };  TimerTask task = new TimerTask() {   @Override   public void run() {    recLen--;    Message message = new Message();    message.what = 1;    handler.sendMessage(message);   }  }; }   方法三 Handler與Message(不用TimerTask) public class timerTask extends Activity{  private int recLen = 11;  private TextView txtView;  public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);    setContentView(R.layout.timertask);    txtView = (TextView)findViewById(R.id.txttime);   Message message = handler.obtainMessage(1);  // Message   handler.sendMessageDelayed(message, 1000);  }   final Handler handler = new Handler(){   public void handleMessage(Message msg){   // handle message    switch (msg.what) {    case 1:     recLen--;     txtView.setText("" + recLen);     if(recLen > 0){      Message message = handler.obtainMessage(1);      handler.sendMessageDelayed(message, 1000);  // send message     }else{      txtView.setVisibility(View.GONE);     }    }    super.handleMessage(msg);   }  }; } 方法四 Handler與Thread(不占用UI線程) public class timerTask extends Activity{  private int recLen = 0;  private TextView txtView;  public void onCreate(Bundle savedInstanceState){   super.onCreate(savedInstanceState);   setContentView(R.layout.timertask);   txtView = (TextView)findViewById(R.id.txttime);      new Thread(new MyThread()).start();   // start thread  }   final Handler handler = new Handler(){   // handle   public void handleMessage(Message msg){    switch (msg.what) {    case 1:     recLen++;     txtView.setText("" + recLen);    }    super.handleMessage(msg);   }  };  public class MyThread implements Runnable{  // thread   @Override   public void run(){    while(true){     try{      Thread.sleep(1000);  // sleep 1000ms      Message message = new Message();      message.what = 1;      handler.sendMessage(message);     }catch (Exception e) {     }    }   }  } } 方法五 Handler與Runnable(最簡單型) public class timerTask extends Activity{  private int recLen = 0;  private TextView txtView;  public void onCreate(Bundle savedInstanceState){   super.onCreate(savedInstanceState);   setContentView(R.layout.timertask);   txtView = (TextView)findViewById(R.id.txttime);      handler.postDelayed(runnable, 1000);  }   Handler handler = new Handler();  Runnable runnable = new Runnable() {   @Override   public void run() {    recLen++;    txtView.setText("" + recLen);    handler.postDelayed(this, 1000);   }  }; }  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved