java - (android studio) mTimer and button -


mtimer.schedule(new timertask()     {         @override         public void run()         {             randomnumber = rand.nextint(8) + 1;             arraylist.add(randomnumber);              runonuithread(new runnable()             {                 @override                 public void run()                 {                     textarraylist.settext("" + arraylist);                     textrandomnumber.settext("" + randomnumber);                      if (gameoptionnumber < arraylist.size())                     {                         layoutgamebutton.setvisibility(view.visible);                          if(enterclicked == false)                         {                             mtimer.cancel();                         }                     }                 }             });         }     }, 3000, 3000); 

...

else if(v == buttonenter)     {         textview output = (textview)findviewbyid(r.id.output);         enterclicked = true;         if(output.gettext().length() != 0)         {             temp = integer.parseint(output.gettext().tostring());             comparenumber(temp);             output.settext("");         }     } 

what trying here number changes every 3 seconds , have press numbers enter button. if don't press enterbutton before 3 seconds until next number comes up, timer should stop.

i created enterclicked , assigned false did everytime enterbutton clicked changes enterclicked true , timer should keep going.

but unfortunately, timer stops though pressed enterbutton.

should place enterclicked @ different place?

please help!!

the problem here when cancel timer not restart or make new timer! http://developer.android.com/intl/pt-br/reference/java/util/timer.html#cancel()

public void cancel ()

cancels timer , scheduled tasks. if there running task not affected. no more tasks may scheduled on timer. subsequent calls nothing.

since have restart timer when enter pressed, kind of need restart timer before next scheduled timer period. when cancel timer make sure make new 1 , start that. (this might lead memory hogging if previous timers not garbage collected.)

also recommend use handlers instead of timertasks. read - http://www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/

all best :)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -