I am not able to stop the Android Timer -


i using following code run timer in android app.

i want stop timer when time reaches

  • 1 minute
  • 2 minute
  • 3 minute

and on. not able understand how it. appreciated.

import android.app.activity; import android.os.bundle; import android.os.handler; import android.view.view; import android.widget.button; import android.widget.textview;  public class mainactivity extends activity {      textview timertextview;     long starttime = 0;      //runs without timer reposting handler @ end of runnable     handler timerhandler = new handler();     runnable timerrunnable = new runnable() {          @override         public void run() {             long millis = system.currenttimemillis() - starttime;             int seconds = (int) (millis / 1000);             int minutes = seconds / 60;             seconds = seconds % 60;              timertextview.settext(string.format("%d:%02d", minutes, seconds));              timerhandler.postdelayed(this, 500);         }     };      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          timertextview = (textview) findviewbyid(r.id.text);          button b = (button) findviewbyid(r.id.button);         b.settext("start");         b.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 button b = (button) v;                 if (b.gettext().equals("stop")) {                     timerhandler.removecallbacks(timerrunnable);                     b.settext("start");                 } else {                     starttime = system.currenttimemillis();                     timerhandler.postdelayed(timerrunnable, 0);                     b.settext("stop");                 }             }         });     }    @override     public void onpause() {         super.onpause();         timerhandler.removecallbacks(timerrunnable);         button b = (button)findviewbyid(r.id.button);         b.settext("start");     }  } 

why not use countdowntimer class?

you can instantiate as:

int bigtime = 1000000000;  //1000 ms after timer ticks (that is, method gets called , so, can update view) countdowntimer countdowntimer = new countdowntimer(bigtime, 1000) {     public void ontick(long millisuntilfinished) {         updatetime();          //you can write code update view in method     }     @override     public void onfinish() {         log.i("get life bro..."," 31 years have passed!");     } }; 

now, in oncreate() method, according clicklisteners on start/stop button, can start or stop timer:

countdowntimer.start();  if(seconds == 0 && minutes > 0) {     // values of seconds , minutes view.     countdowntimer.cancel(); } 

just in case want pause timer , start paused time, can store value of time in milliseconds, stop timer , restart after adding value stored.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -