2012-03-09 96 views
2

我已經通過鏈接http://dewful.com/?tag=basic-android-timer關於android中的計時器應用程序。它工作正常。 我需要添加暫停按鈕來停止計時器和播放按鈕以從我停止的地方再次啓動計時器。我可以完成這項任務嗎?如何在android中暫停計時器?

我的代碼:

long timervalue = 50000; 
CountDownTimer Counter1 = new CountDownTimer(timervalue, 1000) 
{ 
    public void onTick(final long millisUntilFinished) 
    { 
     time.setText(formatTime(millisUntilFinished)); 
     pause.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Counter1.cancel(); 
      } 
     } 
     ); 
     resume.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Counter1.start(); 
       timervalue = Long.parseLong(output); 
       System.out.println("paused timer value resumed"+timervalue); 
       Counter1.onTick(timervalue); 
      } 
     } 
     ); 
    } 
    public void onFinish() 
    { 
     Counter1.cancel(); 
    } 
}; 
public String formatTime(long millis) 
{ 
    output = "00"; 
    seconds = millis/1000; 
    seconds = seconds % 60; 
    System.out.println("seconds here"+seconds); 
    String secondsD = String.valueOf(seconds); 
    System.out.println("secondsD here"+secondsD); 
    if (seconds < 
    10)    secondsD = "0" + seconds; 
    System.out.println("secondsD here in if"+secondsD); 
    output = secondsD; 
    return output; 
} 

在當恢復按鈕被點擊的計時器再次從50秒開始,我不希望像上面的代碼。它應該從我暫停的那一刻開始。請幫我對此...我爲這個苦苦掙扎,因爲1周......

將是幫助真的很感謝..........

+0

查看下面的鏈接...它會幫助你希望。 http://stackoverflow.com/questions/2098642/pausing-stopping-and-starting-resuming-java-timertask-continuously – 2012-03-09 07:28:56

+0

嗨謝謝你的鏈接....我已經通過它..可以你請告訴我如何在我的代碼中應用該代碼? – RaagaSudha 2012-03-09 11:23:10

回答

9
public class TimerActivity extends Activity 
{ 
    EditText e1; 
    MyCount counter; 
    Long s1; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     e1=(EditText)findViewById(R.id.editText1); 
     counter= new MyCount(5000,1000); 
     counter.start(); 
    } 
    public void asdf(View v) 
    { 
     switch(v.getId()) 
     { 
      case R.id.button1:   counter.cancel(); 
      break; 
      case R.id.button2:   counter= new MyCount(s1,1000); 
      counter.start(); 
     } 
    } 
    public class MyCount extends CountDownTimer 
    { 
     public MyCount(long millisInFuture, long countDownInterval) 
     { 
      super(millisInFuture, countDownInterval); 
     } 
     @Override  public void onFinish() 
     { 
      e1.setText("DONE"); 
     } 
     @Override  public void onTick(long millisUntilFinished) 
     { 
      s1=millisUntilFinished; 
      e1.setText("left:"+millisUntilFinished/1000); 
     } 
    } 
} 
0
public void time(long m){ 
    timer=new CountDownTimer(m, 1000) { 

      public void onTick(long millisUntilFinished) { 

       tv_timer.setText(formatTime(millisUntilFinished)); 

      } 

      public void onFinish() { 
       tv_timer.setText("done!"); 
      } 

     }.start(); 

} 

我已經使用這個定時器和我已經把按鈕暫停和使用該

case R.id.bT_PAUSE: 
    String s_time = null; 

    try{ 
    if(Bt_pause.getText().equals("PAUSE")){ 

    s_time=tv_timer.getText().toString(); 
    timer.cancel(); 
    String[] Pause_time=s_time.split(":"); 
    m=Long.parseLong(Pause_time[0].trim()); 
    n=Long.parseLong(Pause_time[1].trim()); 
    m=(m*60)+n; 
    m=m*1000; 
    Bt_pause.setText("RESUME"); 

    }else if(Bt_pause.getText().equals("RESUME")){ 
     //min_longmillis=Long.parseLong(sss); 
     //min_longmillis=min_longmillis*1000*60; 
     //min_longmillis=m; 
     //timer.start(); 
     Toast.makeText(this,String.valueOf(m),Toast.LENGTH_SHORT).show(); 
     time(m); 
     Bt_pause.setText("PAUSE"); 
    } 
    }catch(Exception e){ 
     Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show(); 
    } 
    break; 

這對於數據的時間

public String formatTime(long millis) { 
    String output = "00:00"; 
    long seconds = millis/1000; 
    long minutes = seconds/60; 

    seconds = seconds % 60; 
    minutes = minutes % 60; 

    String sec = String.valueOf(seconds); 
    String min = String.valueOf(minutes); 

    if (seconds < 10) 
     sec = "0" + seconds; 
    if (minutes < 10) 
     min= "0" + minutes; 

    output = min + " : " + sec; 
    return output; 
}//formatTime