java - CountDownTimer not working at all -
i trying run countdowntimer inside thread, won't work..
so in mainactivitys oncreate start on button click that:
public void onclick(final view v) { log.d("main", "onclick"); runonuithread(runner); }
runner
instance of class implements runnable
:
private countdownlatch donesignal = new countdownlatch(1); @override public void run() { log.d("wr", "run"); this.countdown(20); log.d("wr", "waiting"); try { donesignal.await(); } catch (final interruptedexception ex) { log.e("wr", ex.getmessage(), ex); } log.d("wr", "waited"); } private void countdown(int time) { final countdowntimer timer = new countdowntimer(time * 1000, 1000) { @override public void ontick(final long millisuntilfinished) { log.d("wr", "ontick"); } @override public void onfinish() { log.d("wr", "onfinish"); donesignal.countdown(); } }; log.d("wr", "starting"); timer.start(); log.d("wr", "started"); }
with code, application freezes after logging onclick, run , starting. changing runonuithread(runner)
new thread(runner).start();
makes application crash after logging onclick no more output.
some research said countdowntimer needs run on ui-thread due use of handler. freezes.
when remove entire thread stuff , call runner.run();
in buttons onclick (also removing implements runnable in runner instance) following log entries: onclick, run, starting, started, waiting
. nothing happens, if timer not run, no call of ontick method.
how can fix countdowntimer?
countdowntimer
runs on ui thread, , has run on ui thread, because internally uses handler, in turns neead looper. runonuithread(runner);
whatever runner does, runs on ui thread.
private countdownlatch donesignal = new countdownlatch(1);
calling await()
on ui thread block it, , since countdown()
runs on same thread practically dead-locked
app
Comments
Post a Comment