4
Good evening, I did a countdown on my app with the play button so I could start the countdown.... only that I wanted to make a method of pausing time and when I clicked on the play button again it continued from where it left off... how can I do that?
follows my code below:
time class:
package com.allsport.miyonic.allsport;
import android.content.Context;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
public class Tempo extends CountDownTimer {
private View.OnClickListener context;
private TextView tv;
private long tempao;
public Tempo(View.OnClickListener context, TextView tv, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.context = context;
this.tv = tv;
}
@Override
public void onTick(long millisUntilFinished) {
tempao = millisUntilFinished;
tv.setText(getCorretcTimer(true, millisUntilFinished)+":"+getCorretcTimer(false, millisUntilFinished));
}
@Override
public void onFinish() {
tempao -= 1000;
tv.setText(getCorretcTimer(true, tempao)+":"+getCorretcTimer(false, tempao));
Toast.makeText((Context) context, "Fim de jogo", Toast.LENGTH_SHORT);
}
private String getCorretcTimer(boolean isMinute, long millisInFuture){
String aux;
int calen = isMinute ? Calendar.MINUTE : Calendar.SECOND;
Calendar cc = Calendar.getInstance();
cc.setTimeInMillis(millisInFuture);
aux = cc.get(calen) < 10 ? "0"+cc.get(calen) : ""+cc.get(calen);
return (aux);
}
}
activity code:
public class SimplesHome extends AppCompatActivity {
private ImageButton imgButton_play, imgButton_pause, imgButton_1, imgButton_2, vermlho, amarelo;
public TextView valorOne, valorDouble, hora;
public int contador = 0;
public int contador1 = 0;
private Chronometer reloginho;
private EditText casa, fora;
long tempoPausado = 0;
private Tempo time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_simples);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
imgButton_1 = (ImageButton) findViewById(R.id.imgButton_1);
imgButton_2 = (ImageButton) findViewById(R.id.imgButton_2);
vermlho = (ImageButton) findViewById(R.id.btnVermelho);
amarelo = (ImageButton) findViewById(R.id.btnAmarelo);
imgButton_play = (ImageButton) findViewById(R.id.imgButton_play);
imgButton_pause = (ImageButton) findViewById(R.id.imgButton_pause);
reloginho = (Chronometer) findViewById(R.id.chronometer);
valorOne = (TextView) findViewById(R.id.txt_valor1);
valorDouble = (TextView) findViewById(R.id.txt_valor2);
casa = (EditText) findViewById(R.id.lbl_time1);
fora = (EditText) findViewById(R.id.lbl_time2);
hora = (TextView) findViewById(R.id.txtTempo);
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);
time = new Tempo(this, hora, 9901*1000, 1000);
time.start();
Context ini = getApplication();
CharSequence iniciar = "Partida iniciada";
int mostrar = Toast.LENGTH_SHORT;
Toast ir = Toast.makeText(ini, iniciar, mostrar);
ir.show();
}
});
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);
Context parar = getApplication();
CharSequence frase = "Partida pausada";
int rele = Toast.LENGTH_SHORT;
Toast stop = Toast.makeText(parar, frase, rele);
stop.show();
}
});
}
}
Thank you....
so buddy... I did it this way:
time.cancel();
it even stops... but it seems that it does not store the value to be able to continue from where it left off... if I follow my logic as could do?– Nathan
In this case, by clicking again on
Play
it reboots from scratch?– Vitor Subhi
Yeah... I wanted him to start where he left off, you know?
– Nathan
What is happening is that by clicking on
Play
, he arrow the time againtime = new Tempo(this, hora, 9901*1000, 1000);
 time.start();
, you can set it in another location, or create a constraint for case thetime
is reset, it set time again.– Vitor Subhi
following this logic, as I do?
– Nathan
Let’s do different, I found an easier way, in Button
Pause
placetempoPausado = !tempoPausado
, Obs.: Change the variabletempoPausado
for boolean, and in the decrement put the condition, as said above... To pause press the buttonPause
and to de-pause, simply press it again.– Vitor Subhi
Let’s go continue this discussion in chat.
– Vitor Subhi