安卓:暂停/重启定时器或线程 [英] Android: Pause/Resume Timer OR Thread

查看:169
本文介绍了安卓:暂停/重启定时器或线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查几乎SO等答案暂停/重启定时器,而不是找出可能的解决方案。

I have checked almost SO and other answers to pause/resume timer, but not find out the possible solution.

我创建了一个定时器任务,算上工作时间员工,并把它一个TextView里面显示出来。

I have created a Timer task which count the effort time for an employee and put it inside a textview to show.

code如下:

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

其中的 storeeffect 的是我的TextView上所显示的卡在正在运行的线程(主要问题)内的工作时间。我想点击一个按钮暂停工作时间,并在同一按钮继续点击它again.Is有任何其他方式做这种任务的。

where storeeffect is my TextView which shows the effort time which is stuck inside the running thread(main problem). I want to pause the effort time with a button click and resume it at the same button clicked again.Is there any other way to do this kind of task.

我无法暂停/恢复定时器。

I'm unable to pause/resume the timer.

建议大多是AP preciable。

Suggestions are mostly appreciable.

感谢

推荐答案

您的解决方案可能有一个小问题 - 您使用定时器计数的时间间隔,而没有必要。你可以使用即<一个href=\"http://docs.guava-libraries.google$c$c.com/git/javadoc/src-html/com/google/common/base/Stopwatch.html#line.76\"相对=nofollow> 秒表 计数经过的时间。因此,而不是在一个计时器作业添加秒,你可以只从这个计时器经过的时间。要暂停计时器你可以称之为 stopWatch.stop()并启动它,你可以调用 stopWatch.start()

You solution might have a slight problem - you are using timer to count time intervals whereas there is no need to. You could use i.e. StopWatch to count elapsed time. So instead of adding seconds in a timer job you could just get elapsed time from this timer. To pause the timer you could call stopWatch.stop() and to start it, you could call 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();
    }
}

这篇关于安卓:暂停/重启定时器或线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆