Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android計時器,時間計算器的實現方法

android計時器,時間計算器的實現方法

編輯:關於Android編程

需求:默認為"00:00:00",點擊開始按鈕時清零後開始計時,出現如10:28:34。點擊停止的時候停止計時。
問題:使用Calendar DateFormat的方法,不設置時區獲取到的小時是本地時區的(東八區的就是8),設置成GMT標准時區獲取到的時間是12小時(12:00:00),設置24小時制無效。
在開始時間加減各種小時都無效,而且計時只能到12小時就自動跳上去了,始終無法出現默認狀態00:00:00開始計時的效果。
嘗試各種時間設置方法無效後只能自己寫一個根據秒數轉換時間格式字符串的方法了,經過測試是沒問題的,兩位數只能顯示99小時為最大,如需要更大小時數需要改改方法。
另外小時數也不能無限大,超過long數據類型長度會變成負數的,會出現異常的。

顯示效果:

 

 

測試類:

復制代碼 代碼如下:
public class TestTime {
     public static void main(String[] args) {
         TestTime tt = new TestTime();
         tt.showTimeCount(99*3600000+75*1000);
     }

     //時間計數器,最多只能到99小時,如需要更大小時數需要改改方法
     public String showTimeCount(long time) {
         System.out.println("time="+time);
         if(time >= 360000000){
             return "00:00:00";
         }
         String timeCount = "";
         long hourc = time/3600000;
         String hour = "0" + hourc;
         System.out.println("hour="+hour);
         hour = hour.substring(hour.length()-2, hour.length());
         System.out.println("hour2="+hour);

         long minuec = (time-hourc*3600000)/(60000);
         String minue = "0" + minuec;
         System.out.println("minue="+minue);
         minue = minue.substring(minue.length()-2, minue.length());
         System.out.println("minue2="+minue);

         long secc = (time-hourc*3600000-minuec*60000)/1000;
         String sec = "0" + secc;
         System.out.println("sec="+sec);
         sec = sec.substring(sec.length()-2, sec.length());
         System.out.println("sec2="+sec);
         timeCount = hour + ":" + minue + ":" + sec;
         System.out.println("timeCount="+timeCount);
         return timeCount;
     }

 }

實際例子:

復制代碼 代碼如下:
//時間計數器,最多只能到99小時,如需要更大小時數需要改改方法
     public String showTimeCount(long time) {
         if(time >= 360000000){
             return "00:00:00";
         }
         String timeCount = "";
         long hourc = time/3600000;
         String hour = "0" + hourc;
         hour = hour.substring(hour.length()-2, hour.length());

         long minuec = (time-hourc*3600000)/(60000);
         String minue = "0" + minuec;
         minue = minue.substring(minue.length()-2, minue.length());

         long secc = (time-hourc*3600000-minuec*60000)/1000;
         String sec = "0" + secc;
         sec = sec.substring(sec.length()-2, sec.length());
         timeCount = hour + ":" + minue + ":" + sec;
         return timeCount;
     }

     private Handler stepTimeHandler;
     private Runnable mTicker;
     long startTime = 0;

     //開始按鈕
     class startBtnListener implements OnClickListener {
         @Override
         public void onClick(View v) {
             Button b = (Button)v;
             String buttonText = b.getText().toString();
             if("Start".equalsIgnoreCase(buttonText)){
                 b.setText("Stop");
                 // 清零 開始計時
                 stepTimeTV.setText("00:00:00");
                 stepTimeHandler = new Handler();
                 startTime = System.currentTimeMillis();
                 mTicker = new Runnable() {
                     public void run() {
                         String content = showTimeCount(System.currentTimeMillis() - startTime);
                         stepTimeTV.setText(content);

                         long now = SystemClock.uptimeMillis();
                         long next = now + (1000 - now % 1000);
                         stepTimeHandler.postAtTime(mTicker, next);
                     }
                 };
                 //啟動計時線程,定時更新
                 mTicker.run();
             }else{
                 b.setText("Start");
                 //停止計時 Remove any pending posts of Runnable r that are in the message queue.
                 stepTimeHandler.removeCallbacks(mTicker);
             }
         }
     }

用時間格式化的方式測試代碼:

復制代碼 代碼如下:
//開始按鈕 通過Calendar時間設置的方式,無法正常顯示小時為0
     class startBtnListener implements OnClickListener {
         @Override
         public void onClick(View v) {
             Button b = (Button)v;
             String buttonText = b.getText().toString();
             if("Start".equalsIgnoreCase(buttonText)){
                 b.setText("Stop");
                 // 清零 開始計時
                 stepTimeTV.setText("00:00:00");
                 if (mCalendar == null) {
                     mCalendar = Calendar.getInstance();
                     TimeZone tz = TimeZone.getTimeZone("GMT");//GMT+8
                     mCalendar.setTimeZone(tz);
                     mCalendar.get(Calendar.HOUR_OF_DAY);//24小時制
                 }
                 stepTimeHandler = new Handler();
                 //System.uptimeMillis()         //記錄從機器啟動後到現在的毫秒數,當系統進入深度睡眠時,此計時器將會停止
                 //System.currentTimeMillis()   //返回自1970年1月1日到現在的毫秒數,通常用來設置日期和時間
                 //System.elapsedRealtime()   //返回從機器啟動後到現在的毫秒數,包括系統深度睡眠的時間,api裡沒有這個方法
                 //直接取得的是當地時區時間,當地時間跟時區有關,設置GMT後始終多12小時
                 startTime = System.currentTimeMillis();//12*3600000  - 36*3600000減掉或者加上12小時都不行
                 mTicker = new Runnable() {
                     public void run() {
                         //這個減出來的日期是1970年的  時間格式不能出現00:00:00 12:00:00
                         long showTime = System.currentTimeMillis() - startTime;
                         Log.i(TAG,showTime+"");
                         mCalendar.setTimeInMillis(showTime + 13*3600000 + 1000);
                         String content = (String) DateFormat.format(mFormat, mCalendar);
                         stepTimeTV.setText(content);

                         long now = SystemClock.uptimeMillis();
                         long next = now + (1000 - now % 1000);
                         stepTimeHandler.postAtTime(mTicker, next);
                     }
                 };
                 //啟動計時線程,定時更新
                 mTicker.run();
             }else{
                 b.setText("Start");
                 //停止計時 Remove any pending posts of Runnable r that are in the message queue.
                 stepTimeHandler.removeCallbacks(mTicker);
             }
         }
     }

     private Handler stepTimeHandler;
     Calendar mCalendar;
     String mFormat = "yyyy-MM-dd hh:mm:ss";//yyyy-MM-dd
     long startTime = 0;
     private Runnable mTicker;

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved