Медленный секундомер java
Начал изучать java и сделал простой секундомер для android. Но когда запускаю идёт медленнее чем обычный (примерно в полтора раза). И при выключении телефона очень быстро идёт (секунд за 7 проходит минута). В чём может быть проблема? Код:
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Handler;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static android.graphics.Color.*;
public class MainActivity extends AppCompatActivity {
private int seconds = 0;
private boolean running = false;
private TextView myTextView;
private TextView myMillisecondsView;
private Button startBtn;
private Button pauseBtn;
private Button stopBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView)findViewById(R.id.time_view);
myMillisecondsView = (TextView)findViewById(R.id.millisecondsView);
startBtn = findViewById(R.id.startBtn);
pauseBtn = findViewById(R.id.pauseBtn);
stopBtn = findViewById(R.id.stopBtn);
}
public void onClickStart(View view) {
running = true;
runTimer();
startBtn.setClickable(false);
startBtn.setBackgroundColor(LTGRAY);
stopBtn.setClickable(true);
stopBtn.setBackgroundColor(BLUE);
}
public void onClickStop(View view) {
running = false;
seconds = 0;
stopBtn.setClickable(false);
stopBtn.setBackgroundColor(LTGRAY);
startBtn.setClickable(true);
startBtn.setBackgroundColor(BLUE);
}
public void onClickPause(View view) {
running = false;
startBtn.setClickable(true);
startBtn.setBackgroundColor(BLUE);
stopBtn.setBackgroundColor(BLUE);
stopBtn.setClickable(true);
}
private void runTimer() {
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int hours = (seconds/216000)%60;
int minutes = (seconds/60000)%60;
int secon = (seconds/100)%60;
int mili = seconds%100;
String myTime = String.format("%d:%02d:%02d:%02d", hours, minutes, secon, mili);
myTextView.setText(myTime);
myMillisecondsView.setText(String.valueOf(seconds));
if(running){
seconds++;
}
handler.postDelayed(this, 0);
}
});
}
}