2012年4月12日 星期四

[Android]TimeClock 顯示程式開始執行的時間及現在時間


<TimerClock.java>
   1: package com.android.timerclock;
   2:  
   3: import java.text.SimpleDateFormat;
   4:  
   5: import android.app.Activity;
   6: import android.os.Bundle;
   7: import android.os.Handler;
   8: import android.view.View;
   9: import android.view.View.OnClickListener;
  10: import android.widget.Button;
  11: import android.widget.TextView;
  12:  
  13: public class TimerClock extends Activity {
  14:     
  15:     private Long startTime;
  16:     private Handler handler = new Handler();
  17:     private Button b1;
  18:     private TextView tv1, tv2, tv3;
  19:     private SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
  20:  
  21:     @Override
  22:     public void onCreate(Bundle savedInstanceState) {
  23:         super.onCreate(savedInstanceState);
  24:         setContentView(R.layout.main);
  25:         findViews();
  26:         setButtonListeners();
  27:         startRunProcess();
  28:     }
  29:     
  30:     
  31:     private void findViews(){
  32:         tv1 = (TextView) findViewById(R.id.tvStartTime);
  33:         tv2 = (TextView) findViewById(R.id.tvNowTime);
  34:         tv3 = (TextView) findViewById(R.id.timer);
  35:            b1 = (Button)findViewById(R.id.btnExit); 
  36:     }
  37:     
  38:     
  39:     private void startRunProcess() {
  40:         // 利用 System.currentTimeMillis() 讀取系統時間
  41:         startTime = System.currentTimeMillis();
  42:         tv1.setText("起始時間: "+format.format(startTime));
  43:         // 調用 Handler 的 postDelayed 方法
  44:         // 將 要執行的線程對象 updateTimerThread 放入隊列中, 當等待時間 (1000毫秒)結束後, 執行線程對象 updateTimerThread
  45:         handler.postDelayed(updateTimerThread, 1000);
  46:     }
  47:     
  48:     
  49:     private Runnable updateTimerThread = new Runnable() {
  50:         public void run() {
  51:             // 利用 System.currentTimeMillis() 讀取系統時間
  52:             Long nowTime = System.currentTimeMillis();
  53:             tv2.setText("現在時間: "+format.format(nowTime));
  54:             
  55:             Long spentTime = nowTime- startTime;
  56:             //計算目前已過分鐘數
  57:             Long minius = (spentTime/1000)/60;
  58:             //計算目前已過秒數
  59:             Long seconds = (spentTime/1000) % 60;
  60:             tv3.setText("執行時間: "+minius+" 分 "+seconds+" 秒");
  61:             // 調用 Handler 的 postDelayed 方法
  62:             // 將 要執行的線程對象 updateTimerThread 放入隊列中, 當等待時間 (1000毫秒)結束後, 執行線程對象 updateTimerThread
  63:             handler.postDelayed(this, 1000);
  64:         }
  65:     };
  66:  
  67:     private void setButtonListeners(){
  68:         b1.setOnClickListener(new OnClickListener(){
  69:             public void onClick(View v) {
  70:                 // 調用 Handler 的 removeCallbacks 方法, 刪除隊列中未執行的線程對象
  71:                 handler.removeCallbacks(updateTimerThread);
  72:                 android.os.Process.killProcess(android.os.Process.myPid());
  73:             }
  74:         });
  75:     }
  76:  
  77:  
  78: }

<main.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:     android:layout_width="fill_parent"
   4:     android:layout_height="fill_parent"
   5:     android:orientation="vertical"     >
   6:  
   7:     <TextView
   8:         android:id="@+id/tvStartTime"
   9:         android:layout_width="wrap_content"
  10:         android:layout_height="wrap_content"
  11:         android:text="@string/strStartTime"
  12:         android:textSize="30dp" />
  13:  
  14:     <TextView
  15:         android:id="@+id/tvNowTime"
  16:         android:layout_width="wrap_content"
  17:         android:layout_height="wrap_content"
  18:         android:text="@string/strNowTime"
  19:         android:textSize="30dp" />
  20:  
  21:  
  22:     <TextView
  23:         android:id="@+id/timer"
  24:         android:layout_width="wrap_content"
  25:         android:layout_height="wrap_content"
  26:         android:text="@string/strDuration"
  27:         android:textSize="30sp" >
  28:  
  29:     </TextView>
  30:  
  31:  
  32:  
  33:     <Button
  34:         android:id="@+id/btnExit"
  35:         android:layout_width="wrap_content"
  36:         android:layout_height="wrap_content"
  37:         android:text="@string/strExit" />
  38:  
  39: </LinearLayout>


<strings.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <resources>
   3:  
   4:     <string name="hello">Hello World, TimerClock!</string>
   5:     <string name="app_name">TimerClock</string>
   6:     <string name="strExit">離開</string>
   7:     <string name="strDuration">執行時間:0:0</string>
   8:     <string name="strNowTime">現在時間:0:0</string>
   9:     <string name="strStartTime">起始時間:0:0</string>
  10:     
  11: </resources>

沒有留言:

張貼留言