Good evening I did a countdown in my application with the play button to start the count .... but I wanted to make a method to pause the time and when I clicked on the play button again it continued from where it stopped ... how can I do that?
follow 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 ....