Android Timer stops after 0,2

My timer looks like this:

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

                    }

                });

            }

        }

    };

I start it like this:

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

And I stop it like this:

running = false;
counter = 0;

The strange thing is that if I run my app the first time (code was changed and eclipse installs the new apk on the device) and start the timer for the first time it stops counting after 0,2 to 2 seconds. If I close the app (even through the task manager on my phone) this bug is gone. Does anyone know how to fix this issue ?

链接地址: http://www.djcxy.com/p/13008.html

上一篇: 在Webkit和Opera中jquery hasAttr

下一篇: Android Timer在0,2之后停止