android - How to implement pause and resume countdowntimer? -


i have implemented countdowntimer in application.it's working fine.now implemented quit button in app dialog box permission (yes or no).when user press yes quit , no game.now problem how pause countdowntimer when click on quit button , resume on no button in dialog box.

here code:-

public class questionactivity extends activity implements onclicklistener{     private question currentq;     private gameplay currentgame;     public countdowntimer countertimer;     private button nextbtn1, nextbtn2, nextbtn3, nextbtn4, nextbtn5;               @override             public void oncreate(bundle savedinstancestate) {                 super.oncreate(savedinstancestate);                 setcontentview(r.layout.question);                 processscreen();          }                 /**          * configure current game , question          */          private void processscreen()          {         currentgame = ((application)getapplication()).getcurrentgame();         currentq = currentgame.getnextquestion();         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.answer5);         nextbtn5.setenabled(true);         nextbtn5.setonclicklistener(this);         /**          * update question , answer options..          */         setquestions();      }       /**      * method set text question , answers current games      * current question      */     private void setquestions() {         //set question text current question         string question = utility.capitalise(currentq.getquestion());         textview qtext = (textview) findviewbyid(r.id.question);         qtext.settext(question);          //set available options         list<string> answers = currentq.getquestionoptions();         textview option1 = (textview) findviewbyid(r.id.answer1);         option1.settext(utility.capitalise(answers.get(0)));         textview option2 = (textview) findviewbyid(r.id.answer2);         option2.settext(utility.capitalise(answers.get(1)));         textview option3 = (textview) findviewbyid(r.id.answer3);         option3.settext(utility.capitalise(answers.get(2)));         textview option4 = (textview) findviewbyid(r.id.answer4);         option4.settext(utility.capitalise(answers.get(3)));         int score = currentgame.getscore();         string scr = string.valueof(score);         textview score1 = (textview) findviewbyid(r.id.score);         score1.settext(scr);          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's 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's 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();     }       @override     public void onresume() {         super.onresume();     }       @override     public void onclick(view arg0)      {         nextbtn1.setenabled(false);         nextbtn2.setenabled(false);         nextbtn3.setenabled(false);         nextbtn4.setenabled(false);         nextbtn5.setenabled(false);         arg0.setenabled(true);         //log.d("questions", "moving next question");         if(arg0.getid()==r.id.answer5)         {                    button yes, no;             final dialog dialog = new dialog(this, r.style.fullheightdialog); //this reference style above             dialog.setcontentview(r.layout.dialog1); //i saved xml file above yesnomessage.xml             dialog.setcancelable(true);              //to set message             textview message =(textview) dialog.findviewbyid(r.id.tvmessagedialogtext);             message.settext("are sure want exit?");             yes = (button) dialog.findviewbyid(r.id.bmessagedialogyes);             yes.setonclicklistener(new onclicklistener() {             public void onclick(view v)              {              countertimer.cancel();              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();     }          else         {             if(!checkanswer(arg0)) return;            /**          * check if end of game          */         if (currentgame.isgameover()){             //log.d("questions", "end of game! lets add scores..");             //log.d("questions", "questions correct: " + currentgame.getright());             //log.d("questions", "questions wrong: " + currentgame.getwrong());             final handler handle = new handler();             runnable delay = new runnable() {                 public void run() {                     startactivity(new intent(questionactivity.this, endgameactivity.class));                     finish();                 }             };             handle.postdelayed(delay,2000);          }             else             {                 final handler handle = new handler();                 runnable delay = new runnable() {                     public void run() {                    startactivity(new intent(questionactivity.this, questionactivity.class));                    finish();                     }                 };                 handle.postdelayed(delay,2000);                       }                   }               }       @override     public boolean onkeydown(int keycode, keyevent event)     {         switch (keycode)         {         case keyevent.keycode_back :             return true;         }          return super.onkeydown(keycode, event);     }       /**      * check if checkbox has been selected, , if      * has check if correct , update gamescore      */     private boolean checkanswer(view v) {         final button b = (button) v;         string answer = b.gettext().tostring();          countertimer.cancel();          b.setbackgroundresource(r.drawable.ans);          b.setenabled(false);         //log.d("questions", "valid checkbox selection made - check if correct");             if (currentq.getanswer().equalsignorecase(answer))                 {                 b.setbackgroundresource(r.drawable.ansgreen);                 //log.d("questions", "correct answer!");                 toast.maketext(questionactivity.this, "correct answer", toast.length_short).show();                 currentgame.incrementscore();                 }              else{                 b.setbackgroundresource(r.drawable.ansred);                 //log.d("questions", "incorrect answer!");                 toast.maketext(questionactivity.this, "incorrect answer", toast.length_short).show();                 currentgame.decrementscore1();                             }             return true;         }      public void show()      {     countertimer.cancel();               }  } 

hello that-

package com.example.dialogboxwithtimer;  import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.os.bundle; import android.os.handler; import android.os.systemclock; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.widget.toast;  public class mainactivity extends activity {     private textview texttimer;     private button startbutton;     private button pausebutton;     private long starttime = 0l;     private handler myhandler = new handler();     long timeinmillies = 0l;     long timeswap = 0l;     long finaltime = 0l;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          button btn = (button) findviewbyid(r.id.button1);          texttimer = (textview) findviewbyid(r.id.texttimer);          startbutton = (button) findviewbyid(r.id.btnstart);         startbutton.setonclicklistener(new view.onclicklistener() {             public void onclick(view view) {                 starttime = systemclock.uptimemillis();                 myhandler.postdelayed(updatetimermethod, 0);              }         });          pausebutton = (button) findviewbyid(r.id.btnpause);         pausebutton.setonclicklistener(new view.onclicklistener() {             public void onclick(view view) {                 timeswap += timeinmillies;                 myhandler.removecallbacks(updatetimermethod);              }         });          btn.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 alertmessage();                 timeswap += timeinmillies;                 myhandler.removecallbacks(updatetimermethod);             }         });     }      public void alertmessage() {         dialoginterface.onclicklistener dialogclicklistener = new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int which) {                 switch (which) {                 case dialoginterface.button_positive:                     // yes button clicked                      toast.maketext(mainactivity.this, "yes clicked",                             toast.length_long).show();                     break;                  case dialoginterface.button_negative:                     // no button clicked                     // nothing                     starttime = systemclock.uptimemillis();                     myhandler.postdelayed(updatetimermethod, 0);                      toast.maketext(mainactivity.this, "no clicked",                             toast.length_long).show();                     break;                 }             }         };          alertdialog.builder builder = new alertdialog.builder(this);         builder.setmessage("are sure?")                 .setpositivebutton("yes", dialogclicklistener)                 .setnegativebutton("no", dialogclicklistener).show();     }      private runnable updatetimermethod = new runnable() {          public void run() {             timeinmillies = systemclock.uptimemillis() - starttime;             finaltime = timeswap + timeinmillies;              int seconds = (int) (finaltime / 1000);             int minutes = seconds / 60;             seconds = seconds % 60;             int milliseconds = (int) (finaltime % 1000);             texttimer.settext("" + minutes + ":"                     + string.format("%02d", seconds) + ":"                     + string.format("%03d", milliseconds));             myhandler.postdelayed(this, 0);         }      };  } 

and in layout-

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >      <button         android:id="@+id/button1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="popup" />      <relativelayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:background="#000000" >          <textview             android:id="@+id/texttimer"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_above="@+id/btnpause"             android:layout_centerhorizontal="true"             android:layout_marginbottom="37dp"             android:text="00:00:00"             android:textcolor="#ffffff"             android:textsize="40sp" />          <button             android:id="@+id/btnpause"             android:layout_width="90dp"             android:layout_height="45dp"             android:layout_centervertical="true"             android:layout_torightof="@+id/btnstart"             android:text="pause" />          <button             android:id="@+id/btnstart"             android:layout_width="90dp"             android:layout_height="45dp"             android:layout_alignparentleft="true"             android:layout_centervertical="true"             android:layout_marginleft="48dp"             android:text="start" />     </relativelayout>  </linearlayout> 

for more see these url- http://www.androidhub4you.com/2013/04/countdown-demo-in-android-custom-timer.html , http://www.androidhub4you.com/2012/09/alert-dialog-box-or-confirmation-box-in.html

blockquote


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -