Android计时器在0,2 - 2秒后停止

时间:2014-03-13 16:10:26

标签: java android timer

我的计时器如下所示:

runnable = new Runnable() {

        @Override
        public void run() {
            while (running) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (running) {
                            counter += 0.01;
                            String outputStr = String.format("%.2f",
                                    counter);
                            outputStr = outputStr.replace(",", ".");
                            display_time.setText(outputStr);
                        }

                    }

                });

            }

        }

    };

我这样开始:

new Thread(runnable).start();
running = true;

我这样就停止了:

running = false;
counter = 0;

奇怪的是,如果我第一次运行我的应用程序(代码被更改,eclipse在设备上安装新的apk)并且第一次启动计时器,它会在0.2到2秒后停止计数。如果我关闭应用程序(即使通过手机上的任务管理器),这个错误也就消失了。 有谁知道如何解决这个问题?

0 个答案:

没有答案
相关问题