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(); }
source share