Timer with the Chronometer

Asked

Viewed 334 times

1

I’m developing an app and it uses the timer by clicking on play to start and pause it to pause, only when I click again on play he Zera, would have how to keep counting the time?

java code:

public class FutebolSimples extends AppCompatActivity {

    private ImageButton imgButton_play, imgButton_pause, imgButton_1, imgButton_2;
    private TextView txt_valor1, txt_valor2;
    private int contador = 0;
    private int contador1 = 0;
    private Chronometer reloginho;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_futebol_simples);


        imgButton_1 = (ImageButton) findViewById(R.id.imgButton_1);
        imgButton_2 = (ImageButton) findViewById(R.id.imgButton_2);
        imgButton_play = (ImageButton) findViewById(R.id.imgButton_play);
        imgButton_pause = (ImageButton) findViewById(R.id.imgButton_pause);
        reloginho = (Chronometer) findViewById(R.id.chronometer);
        txt_valor1 = (TextView) findViewById(R.id.txt_valor1);
        txt_valor2 = (TextView) findViewById(R.id.txt_valor2);

        imgButton_play.setEnabled(true);
        imgButton_pause.setEnabled(false);
        imgButton_1.setEnabled(false);
        imgButton_2.setEnabled(false);


        imgButton_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(false);
                imgButton_pause.setEnabled(true);
                imgButton_1.setEnabled(true);
                imgButton_2.setEnabled(true);

                reloginho.setBase(SystemClock.elapsedRealtime());
                reloginho.start();
            }
        });

        imgButton_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(true);
                imgButton_pause.setEnabled(false);
                imgButton_1.setEnabled(false);
                imgButton_2.setEnabled(false);

                reloginho.stop();
            }
        });
    }
}

Thank you.

  • Do you want to continue counting from the value you were when you stopped or should you consider the time you spent until you continue?

1 answer

3


To not reset the value you have to add the SystemClock.elapsedRealtime() with the time that was paused with the .stop() using any variable initialized with 0. Example:

long tempoPausado = 0;

Update the variable value when you stop the timer like this:

tempoPausado = reloginho.getBase() - SystemClock.elapsedRealtime();
mChronometer.stop();

So you can use this variable to adjust the timer before starting it:

reloginho.setBase(SystemClock.elapsedRealtime() + tempoPausado);

Good Luck! =)

  • 1

    Thank you.... =)

  • @Nathan For nothing! Need, give a shout! = D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.