Android 中的時間驅動機制 (TimerTask)
Android簡介使用方法開發工具開始寫程式視覺化介面視覺元件對話元件核心物件事件處理資料儲存查詢功能影音功能繪圖功能網路功能衛星地圖特殊功能資源管理裝置管理系統核心問題與回答刷機升級常用軟體Eclipse教學錄影訊息相關網站參考文獻最新修改簡體版English |
專案下載:Clock.zip 程式範例:數位小時鐘檔案:layout/main.xml <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/TextViewTime" android:layout_width="100px" android:layout_height="fill_parent" android:text="Hello!" android:layout_x="120px" android:layout_y="150px" android:textSize="24sp" /> </AbsoluteLayout> 檔案:Clock.java package ccc.android; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.format.Time; import android.widget.TextView; public class Clock extends Activity { protected static final int CLOCK_UPDATE = 0x101; TextView textViewTime; Handler clockHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case Clock.CLOCK_UPDATE: textViewTime.setText(now()); textViewTime.invalidate(); break; } super.handleMessage(msg); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textViewTime = (TextView) findViewById(R.id.TextViewTime); textViewTime.setText(now()); new Thread(new clockThread()).start(); } public String now() { Time time = new Time(); time.setToNow(); return String.format("%02d:%02d:%02d", time.hour, time.minute, time.second); } class clockThread implements Runnable { public void run() { while (!Thread.currentThread().isInterrupted()) { Message message = new Message(); message.what = Clock.CLOCK_UPDATE; Clock.this.clockHandler.sendMessage(message); try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } } 執行結果![]() 改良如果使用 Timer 與 scheduleAtFixedRate() 函數,則上列程式可以簡化如下。 package ccc.android; 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.text.format.Time; import android.widget.TextView; public class ClockTimer extends Activity { TextView textViewTime; private Handler mHandler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: textViewTime.setText(now()); break; } }; }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textViewTime = (TextView) findViewById(R.id.TextViewTime); textViewTime.setText(now()); Timer timer = new Timer(); timer.scheduleAtFixedRate(new ClockTask(), 1, 1000); } public String now() { Time time = new Time(); time.setToNow(); return String.format("%02d:%02d:%02d", time.hour, time.minute, time.second); } private class ClockTask extends TimerTask { @Override public void run() { Message message = new Message(); message.what = 1; mHandler.sendMessage(message); } } } 參考文獻
|
page revision: 7, last edited: 17 Nov 2010 03:36
Post preview:
Close preview