2013-02-07 33 views
0

我已經在一個應用程序中做了定時器,我已經啓動它並且停止並且無法暫停並在它暫停的同一時間恢復。我已經使用以下代碼來安裝應用程序。無法暫停計時器並在應用程序中恢復它?

String Current_click_btn ; 

當暫停BTN點擊保存先前CURENT時間在毫秒級上的簡歷,我會增加與100的值,所以當再次跟帖說以前保存的值開始將它會增加保存的當前時間100。反覆

@Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
     if(v ==Start) 
      { 
       Current_clickbtn="Start"; 
       startTime = System.currentTimeMillis(); 
       mHandler.removeCallbacks(startTimer); 
       mHandler.postDelayed(startTimer, 0); 
      } 

      if(v == Pause) 
      { 
       Current_clickbtn="Pause"; 
       if(Pause.getText().equals("Pause")) 
       { 
        Pause.setText("test"); 
        mHandler.removeCallbacks(startTimer); 
       } 
       else 
       { 
        mHandler.postDelayed(startTimer, 0); 
        Pause.setText(getString(R.string.pause)); 
       } 
      } 


      } 
    private Runnable startTimer = new Runnable() 
{ 
     public void run() { 

      if(Current_clickbtn.equals("Start")) 
      { 
      CurrentTime = System.currentTimeMillis(); 
      elapsedTime = CurrentTime - startTime; 
      } 
      else 
      { 
       elapsedTime = CurrentTime - startTime; 
       CurrentTime+=100; 
      } 
      updateTimer(elapsedTime,ctx); 

       mHandler.postDelayed(this,REFRESH_RATE); 

     } 
}; 

      public void updateTimer (float time,Context ctx){ 
        secs = (long)(time/1000); 
        mins = (long)((time/1000)/60); 
        hrs = (long)(((time/1000)/60)/60); 

        /* Convert the seconds to String 
        * and format to ensure it has 
        * a leading zero when required 
        */ 
        secs = secs % 60; 
        seconds=String.valueOf(secs); 
        if(secs == 0){ 
         seconds = "00"; 
        } 
        if(secs <10 && secs > 0){ 
         seconds = "0"+seconds; 
        } 

        /* Convert the minutes to String and format the String */ 

        mins = mins % 60; 
        minutes=String.valueOf(mins); 
        if(mins == 0){ 
         minutes = "00"; 
        } 
        if(mins <10 && mins > 0){ 
         minutes = "0"+minutes; 
        } 

        /* Convert the hours to String and format the String */ 

        hours=String.valueOf(hrs); 
        if(hrs == 0){ 
         hours = "00"; 
        } 
        if(hrs <10 && hrs > 0){ 
         hours = "0"+hours; 
        } 

        /* Although we are not using milliseconds on the timer in this example 
        * I included the code in the event that you wanted to include it on your own 
        */ 


        System.out.println("***"+hours + ":" + minutes + ":" + seconds); 
        Timetextview=(TextView)((Activity) ctx).findViewById(com.carenet.activity.R.id.textView_time); 
        Timetextview.setText(hours + ":" + minutes + ":" + seconds); 

       } 

pla幫助。

+0

你的處理器已經在UI線程。你的runonuithread是無用的 – njzk2

+0

我編輯了代碼。現在我已經保存了以前的當前時間值,在暫停和恢復時,我將增加保存的值。請在暫停和恢復時間上提供最佳方法 – user1787605

回答

0

你的代碼讀取:

     CurrentTime = System.currentTimeMillis(); 
        elapsedTime = CurrentTime - startTime; 

就是你暫停與否,它顯示的時間,因爲startTime

+0

我已編輯代碼。現在我已經保存了以前的當前時間值,在暫停和恢復時,我將增加保存的值。請在暫停和恢復時間時提示最佳方法 – user1787605