How can i code a time limit in my android game -


i have quiz game android has time limit. want there choices button if click 1 of buttons automatically intent class next level if didnt answer or click of button intent other class, thats why game has time limit. problem dont know how put time limit intent or transfer in class automatically if didnt click of button choices. tried sleep happen clicked correct answer , im on next level class sleep class intented sleep. please me problem. try handler didnt work

public class easyone extends activity { 

button a, b, c; textview timer;

@override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.easyone);     = (button) findviewbyid(r.id.btn_ea1);     b = (button) findviewbyid(r.id.btn_eb1);     c = (button) findviewbyid(r.id.btn_ec1);     a.setonclicklistener(new view.onclicklistener() {     @override               public void onclick(view v) {                 toast.maketext(getapplicationcontext(),"correct!",                         toast.length_short).show();                 intent intent = new     intent(getapplicationcontext(),easytwo.class);                 startactivity(intent);         }     }); }  private runnable task = new runnable() {      public void run() {         handler handler = new handler();         handler.postdelayed(task, 5000);          intent intent = new intent(getapplicationcontext(),timesup.class);             startactivity(intent);      } }; 

you should use handler in order cancel timeout must remove delayed message handler in click listener code.

public class easyone extends activity {  static private handler mhandler = new handler() {     @override     public void handlemessage(message msg) {         super.handlemessage(msg);         if (msg.what == 123) {             ((easyone) msg.obj).ontimeout();         }     } };  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.easyone);     = (button) findviewbyid(r.id.btn_ea1);     b = (button) findviewbyid(r.id.btn_eb1);     c = (button) findviewbyid(r.id.btn_ec1);      message msg = mhandler.obtainmessage(123,this);     mhandler.sendmessagedelayed(msg,5000);      a.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             toast.maketext(getapplicationcontext(),"correct!",                     toast.length_short).show();              mhandler.removemessages(123,this);              intent intent = new intent(getapplicationcontext(),easytwo.class);             startactivity(intent);          }     }); }  private void ontimeout() {     //your code } 

}


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -