Why is my Stopwatch in Android running very slow?

Viewed 31

i tried to create a custom stopwatch application updating time every 10 miliseconds (Format: hh:mm:ss:cs (centiseconds)). The problem is that every 10 seconds in real life, only ~6 seconds passes in my stopwatch. I am using handler.postDelayed(Runnable, 10) to update UI. What is wrong with my code?

private int centiseconds = 0;
private boolean running = false;
// inside onCreate() method:

        timerRunnable = new Runnable() {
            @Override
            public void run() {
                TextView timeView = findViewById(R.id.text_time);
                if (running) {
                    centiseconds++;
                    handler.postDelayed(this, 10);
                }
                int centisecs = centiseconds % 100;
                int seconds = (centiseconds % 6000) / 100;
                int minutes = (centiseconds % 360000) / 6000;
                int hours = (centiseconds / 360000);
                String newTime = String.format(Locale.getDefault(), "%d:%02d:%02d:%02d",
                        hours, minutes, seconds, centisecs);
                timeView.setText(newTime);
            }
        };
    private void onClickStart() {
        if (!running) {
            running = true;
            handler.postDelayed(timerRunnable, 10);
        }
    }
0 Answers
Related