如何在android中暂停计时器?

时间:2012-03-09 07:24:04

标签: android

我已经浏览了关于android中的计时器应用程序的链接http://dewful.com/?tag=basic-android-timer。它工作正常。 我需要添加暂停按钮来停止计时器和一个播放按钮,以便从我停止的位置再次启动计时器。我可以完成该任务吗?

我的代码:

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秒开始,我不希望这样。它应该从我暂停的时间开始。请帮我解决这个问题......一周之后我就一直在努力......

非常感谢帮助..........

2 个答案:

答案 0 :(得分:10)

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);
        }
    }
}

答案 1 :(得分: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