无法暂停计时器并在应用程序中恢复它?

时间:2013-02-07 09:02:34

标签: android timer

我已经在应用程序中创建了计时器,我已经启动它并停止并且无法暂停和恢复它暂停的同一时间。我使用以下代码来重复应用程序。

 String Current_click_btn ;

当单击暂停btn时,以毫秒为单位保存前一个临时时间,并且在恢复时,我将使用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);

                }

帮助。

1 个答案:

答案 0 :(得分:0)

您的代码为:

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

也就是说,您是否暂停,它显示自startTime

以来的时间