Android handler: Handler does not work as expected -
i have button name btnok, inside button have normal method.
my idea after interval of time, method inside button executed automatically.
so create handler delay function. expected programming working this:
here logic:
user can click button ok => after 20s, if user not click button => run method
my prolem 20s calculated total every time run itent.
for examle:
- from activity a, start b (the ok button in b, user must click button in 20s),
- first time, it takes 15s user decides click buton.
- after that, start activity (like loop => b => => b...);
and again, activity b, this time expected method must run after 20s, 5s later (i guess 15s 1st time + 5s after start 2nd time counted total 20s), executed automatically.
@override protected void oncreate(bundle savedinstancestate) { //do value(); //create handler final handler handler = new handler(); //run method automatically after 20s handler.postdelayed(new runnable() { @override public void run() { // after 5s = 5000ms submitresult(); } }, 20000); //wait user clicks, //must start in 20s or submitresult(); executed automatically btn = (button) findviewbyid(r.id.btnok); btn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { submitresult(); } }); }
can 1 me figure out problem? or way auto onclick after interval of time?
i appreciate helps. in advance.
that first handler
running, not new 1 (created in second activity b). if user clicks button, should "cancel" handler, otherwise run submitresult()
twice.
try this:
btn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { handler.removecallbacks(null); submitresult(); } });
Comments
Post a Comment