Android:暂停/恢复计时器或线程

时间:2013-12-21 09:14:41

标签: java android multithreading timer

我已经检查了所有关于如何暂停/恢复计时器的SO答案,但无法找到解决方案。

我创建了一个Timer任务,它计算员工的工作时间,并将其放在TextView中显示。

以下代码:

Timer T = new Timer();
    T.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String workingTime = "Your effort is "
                            + format.format(Double.valueOf(hr)) + ":"
                            + format.format(Double.valueOf(min)) + ":"
                            + format.format(Double.valueOf(sec))
                            + " till now for the day";
                    storeEffort.setText(workingTime);
                    sec++;
                    if (sec > 59) {
                        sec = 0;
                        min = min + 1;
                    }
                    if (min > 59) {
                        min = 0;
                        hr = hr + 1;
                    }
                }
            });
        }
    }, 1000, 1000);

其中 storeEffort 是我的TextView,它显示了在运行线程中遇到的工作时间(主要问题)。我想点击一下按钮暂停努力计时器,并在再次点击同一个按钮时恢复它。还有其他办法可以做这种任务吗?

2 个答案:

答案 0 :(得分:2)

你的解决方案可能有一个小问题 - 你使用计时器来计算时间间隔,而没有必要。您可以使用ie StopWatch来计算已用时间。因此,不是在计时器作业中添加秒,而是可以从此计时器获得经过的时间。要暂停计时器,您可以拨打stopWatch.stop()并启动它,然后拨打stopWatch.start()

看起来像这样:

Stopwatch stopwatch = Stopwatch.createStarted();

void startThreadUpdateTimer(){}
    Timer T = new Timer();
    T.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String workingTime = "Your effort is " + sw.toString() + 
                         " till now for the day";                        
                }
            });
        }
    }, 1000, 1000);
}

public void pause(){
    if(stopwatch.isRunning()){
        stopwatch.stop();
    }
}

public void resume(){
    if(!stopwatch.isRunning()){
        stopwatch.start();
    }
}

答案 1 :(得分:1)

更新解决方案,如果计时器需要每隔一秒从头开始:

public class YourOuterClass extends Activity {
    private YourTimerTask mTimerTask;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button;
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (mTimerTask != null && mTimerTask.isTaskActive()) {
                    mTimerTask.deactivateTimer();
                    mTimerTask = null;
                } else {
                    startTask();
                }

            }
        });
        ...
    }

   private class YourTimerTask extends TimerTask {
       private boolean mIsTimerActive;

       public YourTimer() {
           mIsTimerActive = true;
       }

       public void deactivateTimer() {
           mIsTimerActive = false;
       }

       public boolean isTaskActive() {
           return mIsTimerActive;
       }

       @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String workingTime = "Your effort is "
                            + format.format(Double.valueOf(hr)) + ":"
                            + format.format(Double.valueOf(min)) + ":"
                            + format.format(Double.valueOf(sec))
                            + " till now for the day";
                    if (!mIsTimerActive) {
                        cancel(); // will cancel this timer instance
                    }
                    sec++;
                    if (sec > 59) {
                        sec = 0;
                        min = min + 1;
                    }
                    if (min > 59) {
                        min = 0;
                        hr = hr + 1;
                    }
                }
            });
        }
   }
...
   private void startTask() {
      Timer T = new Timer();
      mTimerTask = new YourTimertask();
      T.scheduleAtFixedRate(mTimerTask, 1000, 1000);
   }
}