handlers - When should we use Looper in Android? -
can please tell me when should use looper in handlers? have codebase in there multiple threads , handlers. looper.prepare()
, looper.loop()
not called of them.
my doubt need looper continously process messages in handlemessage method? if not have looper, won't handlemessage() called when message sent handler? additional purpose looper serving here?
thanks, shamy
class used run message loop thread. threads default not have message loop associated them; create one, call prepare() in thread run loop, , loop() have process messages until loop stopped.
most interaction message loop through handler class.
below there run method of thread
@override public void run() { try { // preparing looper on current thread // current thread being detected implicitly looper.prepare(); log.i(tag, "downloadthread entering loop"); // now, handler automatically bind // looper attached current thread // don't need specify looper explicitly handler = new handler(); // after following line thread start // running message loop , not // exit loop unless problem happens or // quit() looper (see below) looper.loop(); log.i(tag, "downloadthread exiting gracefully"); } catch (throwable t) { log.e(tag, "downloadthread halted due error", t); } }
Comments
Post a Comment