How to start a new countdowntimer in a dialog with the remaining seconds?

In my application, I have a countdown timer and a dialog box in the same class. when someone clicked the close button, a dialog box opens and there are two yes and no buttons in it. I want someone to press the pause button when the timer button is pressed, and if someone does not press the button, resume it with the remaining seconds. I know, for this I need to end this timer and create a new timer with the remaining seconds. But I can’t get the remaining seconds. If anyone knows how to do this, please help me.

Countdown Timer Code -

counterTimer = new CountDownTimer(15000, 1000) { public void onFinish() { if(currentGame.getRound()==20) { nextBtn1.setEnabled(false); nextBtn2.setEnabled(false); nextBtn3.setEnabled(false); nextBtn4.setEnabled(false); nextBtn5.setEnabled(false); final Handler handle = new Handler(); Toast.makeText(QuestionActivity.this, "Time Up", Toast.LENGTH_SHORT).show(); Runnable delay = new Runnable() { public void run() { System.exit(0); } }; handle.postDelayed(delay,3000); } else if(currentGame.getRound()==0) { currentGame.decrementScore(); final Handler handle = new Handler(); Runnable delay = new Runnable() { public void run() { processScreen(); } }; handle.postDelayed(delay,3000); } else if(currentGame.getRound()<=19) { nextBtn1.setEnabled(false); nextBtn2.setEnabled(false); nextBtn3.setEnabled(false); nextBtn4.setEnabled(false); nextBtn5.setEnabled(false); currentGame.decrementScore(); final Handler handle = new Handler(); Toast.makeText(QuestionActivity.this, "Time Up", Toast.LENGTH_SHORT).show(); Runnable delay = new Runnable() { public void run() { processScreen(); } }; handle.postDelayed(delay,3000); } } public void onTick(long millisUntilFinished) { TextView time = (TextView) findViewById(R.id.timers); time.setText( ""+millisUntilFinished/1000); } }; counterTimer.start(); } 

The code for the dialog box is

 if(arg0.getId()==R.id.quit) { Button yes, no; final Dialog dialog = new Dialog(this, R.style.FullHeightDialog); dialog.setContentView(R.layout.dialog1); dialog.setCancelable(true); counterTimer.cancel(); //to set the message TextView message =(TextView) dialog.findViewById(R.id.tvmessagedialogtext); message.setText("Are you sure you want to Exit?"); yes = (Button) dialog.findViewById(R.id.bmessageDialogYes); yes.setOnClickListener(new OnClickListener() { public void onClick(View v) { finish(); startActivity(new Intent(QuestionActivity.this, SplashActivity.class)); } }); no = (Button) dialog.findViewById(R.id.bmessageDialogNo); no.setOnClickListener(new OnClickListener() { public void onClick(View v) { dialog.dismiss(); nextBtn1.setEnabled(true); nextBtn2.setEnabled(true); nextBtn3.setEnabled(true); nextBtn4.setEnabled(true); } }); dialog.show(); } 
0
source share
3 answers

Dialog Code -

  private long remaingtime, starttime = 15000; MyCounter timer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.question); nextBtn1 = (Button) findViewById(R.id.answer1); nextBtn1.setEnabled(true); nextBtn1.setOnClickListener(this); nextBtn2 = (Button) findViewById(R.id.answer2); nextBtn2.setEnabled(true); nextBtn2.setOnClickListener(this); nextBtn3 = (Button) findViewById(R.id.answer3); nextBtn3.setEnabled(true); nextBtn3.setOnClickListener(this); nextBtn4 = (Button) findViewById(R.id.answer4); nextBtn4.setEnabled(true); nextBtn4.setOnClickListener(this); nextBtn5 = (Button) findViewById(R.id.quit); nextBtn5.setEnabled(true); nextBtn5.setOnClickListener(this); timer = new MyCounter(starttime, 1000); timer.start(); } if(arg0.getId() == R.id.quit) { timer.cancel(); final Dialog dialog = new Dialog(this, R.style.FullHeightDialog); dialog.setContentView(R.layout.dialog1); dialog.setCancelable(true); //to set the message TextView message =(TextView) dialog.findViewById(R.id.tvmessagedialogtext); message.setText("Are you sure you want to Exit?"); yes = (Button) dialog.findViewById(R.id.bmessageDialogYes); yes.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(new Intent(QuestionActivity.this, SplashActivity.class)); finish(); } }); no = (Button) dialog.findViewById(R.id.bmessageDialogNo); no.setOnClickListener(new OnClickListener() { public void onClick(View v) { dialog.dismiss(); timer = new MyCounter(remaingtime, 1000); timer.start(); nextBtn1.setEnabled(true); nextBtn2.setEnabled(true); nextBtn3.setEnabled(true); nextBtn4.setEnabled(true); } }); dialog.show(); } 

Count Down Timer Code -

 public class MyCounter extends CountDownTimer { public MyCounter(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onTick(long millisUntilFinished) { remaingtime = millisUntilFinished; time = (TextView) findViewById(R.id.timers); time.setText(""+millisUntilFinished/1000); } @Override public void onFinish() { if(currentGame.getRound()==20) { nextBtn1.setEnabled(false); nextBtn2.setEnabled(false); nextBtn3.setEnabled(false); nextBtn4.setEnabled(false); nextBtn5.setEnabled(false); final Handler handle = new Handler(); Toast.makeText(QuestionActivity.this, "Time Up", Toast.LENGTH_SHORT).show(); Runnable delay = new Runnable() { public void run() { System.exit(0); } }; handle.postDelayed(delay,3000); } else if(currentGame.getRound()==0) { currentGame.decrementScore(); final Handler handle = new Handler(); Runnable delay = new Runnable() { public void run() { processScreen(); } }; handle.postDelayed(delay,3000); } else if(currentGame.getRound()<=19) { nextBtn1.setEnabled(false); nextBtn2.setEnabled(false); nextBtn3.setEnabled(false); nextBtn4.setEnabled(false); nextBtn5.setEnabled(false); currentGame.decrementScore(); final Handler handle = new Handler(); Toast.makeText(QuestionActivity.this, "Time Up", Toast.LENGTH_SHORT).show(); Runnable delay = new Runnable() { public void run() { processScreen(); } }; handle.postDelayed(delay,3000); } } } 
+1
source

try to do it

 private final long interval = 1000; long millisLeft = 90000; public class MalibuCountDownTimer extends CountDownTimer { public MalibuCountDownTimer(long startTime, long interval) { super(startTime, interval); } @Override public void onFinish() { Toast.makeText(context, "Time up!", Toast.LENGTH_LONG).show(); } @Override public void onTick(long millisUntilFinished) { millisLeft = millisUntilFinished; System.out.println("The milies left is" + millisLeft); } } @Override public void onResume() { super.onResume(); countDownTimer = new MalibuCountDownTimer(millisLeft, interval); countDownTimer.start(); } public void onPause() { super.onPause(); countDownTimer.cancel(); } 
0
source

Just count the seconds at each tick and use them later to create a new timer.

0
source

Source: https://habr.com/ru/post/949333/


All Articles